/// Returns a Boolean representation for [arg], which must /// be a String or bool. bool convertToBool(dynamic arg) { if (arg isbool) return arg; if (arg isString) return arg == 'true'; throw ArgumentError('Cannot convert $arg to a bool.'); }
泛型
Dart支持泛型类型,如List(整数列表)或List(任何类型的对象列表)
基本类型转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// String -> int var one = int.parse('1'); assert(one == 1);
String say(String from, String msg, [String device]) { var result = '$from says $msg'; if (device != null) { result = '$result with a $device'; } return result; }
默认参数值
1 2 3 4 5
/// Sets the [bold] and [hidden] flags ... void enableFlags({bool bold = false, bool hidden = false}) {...}
// bold will be true; hidden will be false. enableFlags(bold: true);
/// Returns a function that adds [addBy] to the /// function's argument. Function makeAdder(num addBy) { return (num i) => addBy + i; }
void main() { // Create a function that adds 2. var add2 = makeAdder(2);
// Create a function that adds 4. var add4 = makeAdder(4);
assert(add2(3) == 5); assert(add4(3) == 7); }
makeAdder方法中的代码片段会持有外部传入的addBy
Type test operators
Operator
Meaning
as
Typecast (also used to specify library prefixes)
is
True if the object has the specified type
is!
False if the object has the specified type
??= 当值为空时,才赋值
expr1 ?? expr2
expr1为空才计算expr2
?. 当为空时,整个表达式结果为空
级联
1 2 3 4
querySelector('#confirm') // Get an object. ..text = 'Confirm'// Use its members. ..classes.add('important') ..onClick.listen((e) => window.alert('Confirmed!'));
for in
1 2 3 4
var collection = [0, 1, 2]; for (var x in collection) { print(x); // 0 1 2 }
try catch
throw
1 2 3
throw FormatException('Expected at least 1 section'); throw'Out of llamas!'; void distanceTo(Point other) => throw UnimplementedError();
classEmployeeextendsPerson{ // Person does not have a default constructor; // you must call super.fromJson(data). Employee.fromJson(Map data) : super.fromJson(data) { print('in Employee'); } }
默认参数
1 2 3 4 5 6 7 8 9 10
Point.fromJson(Map<String, num> json) : x = json['x'], y = json['y'] { print('In Point.fromJson(): ($x, $y)'); }
// Define two calculated properties: right and bottom. numget right => left + width; set right(num value) => left = value - width; numget bottom => top + height; set bottom(num value) => top = value - height; }
void main() { var rect = Rectangle(3, 4, 20, 15); assert(rect.left == 3); rect.right = 12; assert(rect.left == -8); }
classA{ // Unless you override noSuchMethod, using a // non-existent member results in a NoSuchMethodError. @override void noSuchMethod(Invocation invocation) { print('You tried to use a non-existent member: ' + '${invocation.memberName}'); } }
var names = <String>['Seth', 'Kathy', 'Lars']; var pages = <String, String>{ 'index.html': 'Homepage', 'robots.txt': 'Hints for web robots', 'humans.txt': 'We are people, not machines' };
To specify one or more types when using a constructor, put the types in angle brackets (<...>) just after the class name. For example:
var names = List<String>(); names.addAll(['Seth', 'Kathy', 'Lars']); var nameSet = Set<String>.from(names);