1.字符串模板
'${3 + 2}' // '5''${"word".toUpperCase()}' // 'WORD''$myObject' // The value of myObject.toString()
2.可空变量
NOTICE:
- dart2.2开始可以将
?添加到该类型中以表示变量可能为空。- 省略赋值null,因为在dart未赋值变量的默认值为null。
// int a = null; // INVALID in null-safe Dart.(错误写法)// dart2.2开始可以将 `?` 添加到该类型中以表示变量可能为空.// 省略赋值null,因为在dart未赋值变量的默认值:nullint? a = null; // Valid in null-safe Dart.int? a; // The initial value of a is null.
3.感知空的操作符
/// `??=` 操作符仅在该变量当前为空值时才将右侧的值分配给变量int? a; // = nulla ??= 3; print(a); // <-- Prints 3.a ??= 5; print(a); // <-- Still prints 3./// 若 `左侧表达式` 值为空,在这种情况下,它将返回其右侧的表达式;否则它将返回其左侧的表达式。print(1 ?? 3); // <-- Prints 1.print(null ?? 12); // <-- Prints 12.
4.有条件的财产访问
myObject?.someProperty;// 两种等价: 上面可以理解为语法糖形式(myObject != null) ? myObject.someProperty : null;// 多层级调用myObject?.someProperty?.someMethod()
5.集合字面
// List<String>// 泛型: <String>[]final aListOfStrings = ['one', 'two', 'three'];// Set<String> or <String>{}final aSetOfStrings = {'one', 'two', 'three'};// Map<String, int> or <String, double>{}final aMapOfStringsToInts = {'one': 1,'two': 2,'three': 3,};
6.Cascades 级联
var button = querySelector('#confirm');button.text = 'Confirm';button.classes.add('important');button.onClick.listen((e) => window.alert('Confirmed!'));// 多个操作连续调用执行 `..`querySelector('#confirm')..text = 'Confirm'..classes.add('important')..onClick.listen((e) => window.alert('Confirmed!'));
