一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第18天,点击查看活动详情。
Dart 语法糖
-
字符串插值:
"${expression}" -
避空运算符:
????=??用于赋值,若左边表达式为空,执行右边表达式??=仅当左边表达式为空时,进行赋值int a; int b = 5; String str = a.toString() ?? b.toString() print(str)//<-- prints "5" a?? = 3; a?? = 5; print(a); //<-- prints 3. -
条件属性访问
?.- 用户保护可能为空的属性正常访问
- 与kotlin的使用方法一致
myObject?.someProperty //上述等效代码 (myObject!=null)?myObject.someProperty:null -
集合字面量,Dart内置对list、map、set的支持
//基础用法--自动识别类型 final aListOfStrings = ['one','two','three']; // List类型 final aSetOfStrings = {'one','two','three'}; // Set类型 final aMapOfStringsToInts = {'one':1, 'two':2, 'three':3}; // Map类型 //定义空对象--自己定义类型 final aListOfInts = <int>[]; final aSetOfInts = <int>{}; final aMapOfIntToDouble = <int,Double>{};
```
-
箭头语法
=>这是一种定义函数的方法,右侧的表达式作为函数的函数体执行并返回相应返回值String joinWithCommas(List<String> strings) => strings.join(','); -
级连
..对象在调用方法时,使用级连操作符,可以将调用方法的返回值修改为对象本身,从而简化代码量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!')); -
getter setter
-
可选位置参数 按照顺序传入参数,
[]中的参数可以不传void printParam(int a, [int b, int c, int d, int e]){ print('${a}'); if(b!=null)print('${b}'); if(c!=null)print('${c}'); if(d!=null)print('${d}'); if(e!=null)print('${e}'); } void printParam(int a, [int b=1, int c=2, int d=3, int e=4]){ print('${a}'); if(b!=null)print('${b}'); if(c!=null)print('${c}'); if(d!=null)print('${d}'); if(e!=null)print('${e}'); } -
可选命名参数
{}内的参数可以通过,参数名直接赋值其中某个参数,注意这里的可选参数只能通过参数名:值的方式进行传参void printParam(int a, {int b, int c, int d, int e}){ print('${a}'); if(b!=null)print('${b}'); if(c!=null)print('${c}'); if(d!=null)print('${d}'); if(e!=null)print('${e}'); } void printParam(int a, {int b=1, int c=2, int d=3, int e=4}){ print('${a}'); if(b!=null)print('${b}'); if(c!=null)print('${c}'); if(d!=null)print('${d}'); if(e!=null)print('${e}'); } void main(){ printParam(1,c:3) } -
异常 在Dart代码中,方法不会声明他们会抛出的异常,也不需要捕获任何异常
-
使用
throw抛出异常,使用rethrow再捕获异常无法完全处理后继续抛出 -
Dart提供了Exception和Error类型供使用
throw Exception('error.') -
Dart也可以抛出任意类型的非空对象
throw 'Error..' -
需要使用
tryoncatchfinally来处理异常,try用来声明可能抛出异常的代码,on用来过滤特定的异常,catch可以捕获到异常对象,finally来执行最后的处理操作try{ }on OutOfLlamasException{ }on Exception catch(e){ print(e) }catch(e){ print(e) }
-
-
构造函数中的
this在定义构造函数中使用this.propertyName,可以方便的调用构造方法对类进行赋值class TestClass{ int mInt; String mString; double mDouble; TestClass(this.mInt); TestClass([this.mInt,this.mString]) TestClass({this.mInt,this.mString,this.mDouble}) } -
Initializer lists 用于在构造函数前执行的一些操作,如未赋值的final字段,在构造函数前进行赋值
class FirstTwoLetters { final String letterOne; final String letterTwo; // Create a constructor with an initializer list here: FirstTwoLetters(String word): assert(word.length>0), letterOne=word.substring(0,1), letterTwo=word.substring(1,2){ print('$letterOne $letterTwo'); } } -
命名构造方法 为了使一个类可以具有多个构造函数,需要使用命名构造方法这种语法来完成
class Color { int red; int green; int blue; Color(this.red, this.green, this.blue); // Create a named constructor called "black" here: Color.black(){ this.red = 0; this.green = 0; this.blue = 0; } } //命名构造方法调用 Color colorObj = Color.black() -
工厂构造方法 可以通过在父类中定义工厂构造方法,调用方法来获取不同的子类对象;
- 工厂方法声明:
factory ClassName.methodName()
class Square extends Shape {} class Circle extends Shape {} class Shape { Shape(); factory Shape.fromTypeName(String typeName) { if (typeName == 'square') return Square(); if (typeName == 'circle') return Circle(); print('I don't recognize $typeName'); return null; } } - 工厂方法声明:
-
重定向构造方法 当创建构造函数时,只是重定向该类的另一个构造方法时,使用这个语法;与kotlin中使用的语法相同,在构造函数后使用
:+构造函数class Color { int red; int green; int blue; Color(this.red, this.green, this.blue); Color.black():this(0,0,0); } -
Const构造方法 如果类的对象永远不会改变,通过使用Const构造方法,让这些对象成为编译时常量。使用
const标记构造方法,并且使所有的属性都是final的class ImmutablePoint { const ImmutablePoint(this.x, this.y); final int x; final int y; static const ImmutablePoint origin = ImmutablePoint(0, 0); } -
类的call方法 类实现了call()函数,那么这个类的实例可以当做方法来调用。
class TestCallClass{ TestCallClass(); call(String str)=>'call is executed.. $str'; } void main(){ var testCall = TestCallClass(); var result = testCall("test"); print(result); }