有两种类型: int 和 double
int类型的值分两种:
(1). Dart VM上, 取值区间: -2^63 to 2^63 - 1
(2). 被编译成JavaScript, 取值区间: -2^53 to 2^53 - 1
var x = 1;
var hex = 0xDEADBEEF;
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
Dart字符串是UTF-16 code序列, 可以使用双引号""或单引号’'创建
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s1 = 'a1'
var s2 = 'a2'
assert(s1 + s2 == 'a1a2');
你可以使用 ${expression}
镶嵌一个表达式或者变量, 如果是个变量还可以省略{}
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' == 'Dart has string interpolation, ' + 'which is very handy.');
注意
: 如果两个字符串有相同的UTF-16 code序列, 则两个字符串==
使用 """.."""
'''..'''
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line 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
bool 只有两个值: true
false
Dart 是类型安全的, 所以除非值或者表达式为true, 否则都是false
数组, 这个不管是学过什么语言的都应该相当熟悉了
Dart创建的数组默认都是可变的
var vegetables = List();
var fruits = [];
fruits.add('kiwis');
// 增加多个
fruits.addAll(['grapes', 'bananas']);
assert(fruits.length == 5);
// 移除单个
var appleIndex = fruits.indexOf('apples');
fruits.removeAt(appleIndex);
assert(fruits.length == 4);
// 移除一组
fruits.clear();
assert(fruits.length == 0);
var fruits = ['apples', 'oranges'];
assert(fruits[0] == 'apples');
assert(fruits.indexOf('apples') == 0);
var fruits = ['bananas', 'apples', 'oranges'];
fruits.sort((a, b) => a.compareTo(b));
assert(fruits[0] == 'apples');
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
字典
Map<String, String> gifts = {'first': 'partridge'};
gifts['fourth'] = 'calling birds'; // Add a key-value pair
var gifts = {'first': 'partridge'};
assert(gifts['fifth'] == null);
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
在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)); // ♥ ? ? ? ? ?
Symbol对象表示Dart程序中声明的运算符或标识符。您可能永远不需要使用符号,但它们对于按名称引用标识符的API非常有用,因为缩小会更改标识符名称而不会更改标识符符号。
https://api.dartlang.org/stable/2.1.0/dart-core/Symbol-class.html
tips: