Introduction
Like all programming languages, dart has its built-in language types. These built-in types are inherited from Object. Of course, these built-in types are the foundation of the dart language. Only by mastering these built-in types can you be handy when using the dart language.
Today I will explain to you the built-in types of the dart language.
Null
In dart, null is used to represent empty. So what is the relationship between null and Null?
Null is a class, first look at the definition of Null:
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 that the string representation of the Null type is null. The corresponding null is a keyword, which corresponds to the Null class.
number
The corresponding class of numbers in dart is num, which has two subclasses, int and double.
int represents an integer not greater than 64 bits. Because dart can run on different platforms, the range represented by different platforms is also different.
On native platforms, such as android or IOS platforms, the range of int can reach -2^63 to 2^63-1. But in the web environment, the range that can be represented is -2^53 to 2^53-1.
The corresponding double represents the floating-point type.
For numbers, basic operation operators like +, -, / and * are all defined in the num class. Of course there are some other conventional operators.
If you need more complex calculations, you can use the dart:math library.
Here are a few examples of the use of numbers:
int age =18;
int number= 20;
double money = 10.1;
String
String is a type that is often used. The corresponding class of strings in dart is String. It can also be expressed directly as a literal as follows:
var name ='jack';
var site ="www.flydean.com";
Strings can be expressed in single or double quotes. The string in dart uses UTF-16 encoding.
In the string in dart, you can also carry variable values, and its format is ${expression}.
var age=10;
var words ='your age is ${age}!';
Two strings can be compared with == to compare whether they are equal. The character comparison is the corresponding character encoding sequence. If the character encoding sequence is equal, then the corresponding string is equal.
You can use + to concatenate strings.
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, the character representation in string is the character itself. If you want to convert it into its original meaning, you can add r in front of the string:
var string3 =r'this is line one \n this is line two';
Boolean value
Boolean values are represented by bool in dart. The bool value has only two string representations, which are true and false.
Because dart is type-safe, that is to say, when you need to use the bool type, you can't use other types instead.
For example, if we want to judge whether the string is empty, we can judge like this:
var name='';
if(name.isEmpty){
do something
}
List
The list in dart is represented by List. Of course, the following literals can also be used directly:
var list = [1, 2, 3];
The index of the list starts from 0 and ends with length-1.
Starting from dart 2.3, the extension symbol... and the nullable extension symbol...? are introduced. The extension symbol can be used to expand a known list into its corresponding elements, so that the combination of the list can be conveniently constructed:
var list = [1, 2, 3];
var list2 = [0, ...list];
Dart provides a very magical function, that is, you can use if and for statements in the process of building a list to dynamically generate elements in the list:
var nav = [
'Home',
'Furniture',
'Plants',
if (promoActive) 'Outlet'
];
or:
var listOfInts = [1, 2, 3];
var listOfStrings = [
'#0',
for (var i in listOfInts) '#$i'
];
set and map
The set in dart is represented by Set.
set represents a collection of non-repeated elements, as shown below:
var names = {'jack', 'mark', 'max'};
The mapping in dart is represented by Map.
The creation of a map is very similar to a set, but contains key and value:
var students = {'jack':18, 'mark':19, 'max':20};
You can find that set and map are very similar, so the question is, how to represent an empty set or map?
Because the elements in the set are single, and the elements in the map are key-value pairs, we can express it like this:
var names = <String>{};
var gifts = Map<String, String>();
But if the type is not specified, then a map is created by default:
var map = {};
To get the value in the map, you can use it like this:
var gifts = Map<String, String>();
gifts['first'] = 'partridge';
gifts['second'] = 'turtledoves';
Both map and set support extensions... and nullable extensions...?, and also support if and for operations in sets.
This article has been included in http://www.flydean.com/02-dart-buildin-type/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。