目錄
系統內建類型
- numbers
- strings
- booleans
- lists (數組)
- maps (字典)
- runes (用於字符串中表示Unicode字符)
- symbols (標識符, 編譯時常量)
1. Numbers
有兩種類型: int 和 double
1.1 int
int類型的值分兩種:
(1). Dart VM上, 取值區間: -2^63 to 2^63 - 1
(2). 被編譯成JavaScript, 取值區間: -2^53 to 2^53 - 1
var x = 1;
var hex = 0xDEADBEEF;
1.2 double
64位(雙精度)浮點型數據
var y = 1.1;
var exponents = 1.42e5;
int double都是num的子類, num的子類還包括一些操作符(= - * /)和abs(), ceil(), floor()等, 位操作符(>>)被定義再int類中,
其他的參考: https://api.dartlang.org/stable/2.1.0/dart-math/dart-math-library.html
note: Dart2.1之後, int可以自動轉成double, 2.1之前會報錯
double z = 1; // z = 1.0.
類型間的轉換:
// String -> int
var one = int.parse('1');
assert(one == 1);
// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);
// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
int類型位操作:
assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 >> 1) == 1); // 0011 >> 1 == 0001
assert((3 | 4) == 7); // 0011 | 0100 == 0111
2. Strings
Dart字符串是UTF-16 code序列, 可以使用雙引號""或單引號’'創建
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
2.1. 合併字符串可以使用 + 操作符
var s1 = 'a1'
var s2 = 'a2'
assert(s1 + s2 == 'a1a2');
2.2. 字符串鑲嵌
你可以使用 ${expression}
鑲嵌一個表達式或者變量, 如果是個變量還可以省略{}
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' == 'Dart has string interpolation, ' + 'which is very handy.');
注意
: 如果兩個字符串有相同的UTF-16 code序列, 則兩個字符串==
2.3. 創建多行字符串
使用 """.."""
'''..'''
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
2.4. “raw” string
var s = r'In a raw string, not even \n gets special treatment.';
\n不會被轉義, 即字符串不會換行
更多: https://www.dartlang.org/guides/libraries/library-tour#strings-and-regular-expressions
3. Booleans
bool 只有兩個值: true
false
Dart 是類型安全的, 所以除非值或者表達式爲true, 否則都是false
4. Lists
數組, 這個不管是學過什麼語言的都應該相當熟悉了
Dart創建的數組默認都是可變的
4.1 創建數組
var vegetables = List();
var fruits = [];
4.2 添加數據
fruits.add('kiwis');
// 增加多個
fruits.addAll(['grapes', 'bananas']);
assert(fruits.length == 5);
4.2 移除數據
// 移除單個
var appleIndex = fruits.indexOf('apples');
fruits.removeAt(appleIndex);
assert(fruits.length == 4);
// 移除一組
fruits.clear();
assert(fruits.length == 0);
4.3 使用indexOf()獲取object在數組中的索引
var fruits = ['apples', 'oranges'];
assert(fruits[0] == 'apples');
assert(fruits.indexOf('apples') == 0);
4.4 使用sort() 對list排序
var fruits = ['bananas', 'apples', 'oranges'];
fruits.sort((a, b) => a.compareTo(b));
assert(fruits[0] == 'apples');
4.5 泛型
Lists are parameterized types, so you can specify the type that a list should contain:
var fruits = List<String>();
fruits.add('apples');
var fruit = fruits[0];
assert(fruit is String);
fruits.add(5); // Error: 'int' can't be assigned to 'String'
Generics: https://www.dartlang.org/guides/language/language-tour#generics
Collections: https://www.dartlang.org/guides/libraries/library-tour#collections
5. Maps
字典
Map<String, String> gifts = {'first': 'partridge'};
5.1. Map默認創建的都是可變的
gifts['fourth'] = 'calling birds'; // Add a key-value pair
5.2. 用不存在的key從map中取值value = null
var gifts = {'first': 'partridge'};
assert(gifts['fifth'] == null);
5.3. 創建不可變的map, 使用const
final constantMap = const {
2: 'helium',
10: 'neon',
18: 'argon',
}; // constantMap[2] = 'Helium'; // Uncommenting this causes an error.
https://api.dartlang.org/stable/2.1.0/dart-core/Map-class.html
https://www.dartlang.org/guides/language/language-tour#generics
6. Runes
在Dart中, Runs是UTF-32 code的字符串
var clapping = '\u{1f44f}';
print(clapping); // ?
print(clapping.codeUnits); // [55357, 56399]
print(clapping.runes.toList()); // [128079]
Runes input = new Runes(
'\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}');
print(new String.fromCharCodes(input)); // ♥ ? ? ? ? ?
7. Symbols
Symbol對象表示Dart程序中聲明的運算符或標識符。您可能永遠不需要使用符號,但它們對於按名稱引用標識符的API非常有用,因爲縮小會更改標識符名稱而不會更改標識符符號。
https://api.dartlang.org/stable/2.1.0/dart-core/Symbol-class.html
tips: