当前位置:网站首页>Dart series: built in types in dart language

Dart series: built in types in dart language

2021-11-25 17:56:44 flydean

brief introduction

Like all programming languages ,dart There are built-in language types , These built-in types inherit from Object, Of course, these built-in types are dart The basis of language , Only by mastering these built-in types can we use dart Language is handy .

Let's talk about it today dart The built-in type of language .

Null

stay dart of use null To represent null . that null and Null What does it matter ?

Null Is a class , Let's take a look at Null The definition of :

class Null {
  factory Null._uninstantiable() {
    throw UnsupportedError('class Null cannot be instantiated');
  }

  external int get hashCode;

  /** Returns the string `"null"`. */
  String toString() => "null";
}

You can see Null Type of string It means null. And the corresponding null It's a keyword , It corresponds to Null class .

Numbers

dart The corresponding class of numbers in is num, He has two subcategories , Namely int and double.

int No greater than 64 bits The integer of . because dart Can run on different platforms , Therefore, the scope represented by different platforms is also different .

On native platform , such as android perhaps IOS platform ,int It can reach -2^63 To 2^63 - 1. But in web Environment , The representable range is -2^53 To 2^53 - 1.

double The corresponding represents the floating point type .

For numbers , The basic operators are like +, -, / and * It's all defined in num Class . Of course, there are other regular operators .

If more complex operations are needed , You can use dart:math library .

Here are some examples of the use of numbers :

int age =18;
int number= 20;
double money = 10.1;

character string

String is a type that is often used .dart The class corresponding to the string in is String. It can also be expressed directly in literal terms as follows :

var name ='jack';
var site ="www.flydean.com";

Strings can be expressed in single or double quotation marks .dart The string in is UTF-16 code .

dart In the string in , You can also bring variable values , His format is ${expression}.

var age=10;
var words ='your age is ${age}!';

Two strings can be used == To see if it's equal , The character comparison is the corresponding character coding sequence , If the character encoding sequences are equal , Then the corresponding strings are equal .

String connection can use +.

var hello ="hello " + "word";

Another way to create a string is to use three single quotes or three double quotes .

var string1= '''
this is a string!
''';

var string2 = """
this is string again!
""";

By default string The character representation in is the character itself , If you want to convert it to its original meaning , You can prefix the string with r:

var string3 =r'this is line one \n this is line two';

Boolean value

Boolean values are in dart of use bool To express .bool The value is only represented by two strings , Namely true and false.

because dart It's type safe , That is, when you need to use bool When it comes to type , No other type can replace .

For example, we need to determine whether the string is empty , It can be judged that :

var name='';
if(name.isEmpty){
    do something
}

list

dart The list in... Is used List To express , Of course, you can also directly use the following literal to express :

var list = [1, 2, 3];

List index from 0 Start , With length-1 end .

from dart 2.3 Start , The extension character is introduced ... And nullable extenders ...? , The extender can be used to put a known list Expand to its corresponding element , Thus, it is convenient to build list The combination of :

var list = [1, 2, 3];
var list2 = [0, ...list];

dart Provides a very magical function , Is that you can build list Used in the process of if and for sentence , To generate dynamically list The elements in :

var nav = [
  'Home',
  'Furniture',
  'Plants',
  if (promoActive) 'Outlet'
];

perhaps :

var listOfInts = [1, 2, 3];
var listOfStrings = [
  '#0',
  for (var i in listOfInts) '#$i'
];

set and map

dart The set in is used Set To express .

set Represents a collection of non repeating elements , As shown below :

var names = {'jack', 'mark', 'max'};

dart The mapping in Map To express .

Map The creation and set Is very similar , But it includes key and value:

var students = {'jack':18, 'mark':19, 'max':20};

You can see ,set and map It's very similar , So here comes the question , How to express an empty set perhaps map Well ?

because set The elements in are single , and map The element in is a key value pair , So we can say :

var names = <String>{};
var gifts = Map<String, String>();

But if you don't specify the type , Then the default is to create a map:

var map = {};

To get map The value in , It can be used in this way :

var gifts = Map<String, String>();
gifts['first'] = 'partridge';
gifts['second'] = 'turtledoves';

map and set All support extensions ... And nullable extensions ...? , It also supports if and for operation .

This article has been included in http://www.flydean.com/02-dart-buildin-type/

The most popular interpretation , The deepest dry goods , The most concise tutorial , There are so many tricks you don't know about waiting for you to discover !

Welcome to my official account. :「 Program those things 」, Know technology , Know you better !

版权声明
本文为[flydean]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/11/20211109094145404b.html

随机推荐