Flutter dart 语法

常用语法表达式

1
2
3
4
5
6
7
8
9
10
11
12
//----??表达式----
String a;
print(a??'defaul value'); //a为null就打印右边的值,不为空就左边
//----函数缩写----
(){print()}//回掉中只含有一句可以缩写成如下
()=>print()
//-----dynamic 相当于object类型------
Map<String,dynamic> data={}; //初始化map
data['key']="value";//赋值
String a=data['key']; //取值
//----list 如何转 json
String jsonStr= jsonEncode(<List>);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class DraftBean {
String title = "";
String summary = "";
SoftAllBean soft = SoftAllBean();
List<StepBean> steps = [];

DraftBean(); //无参构造

DraftBean.fromJson(Map<String, dynamic> json) //自定义初始化方法
: title = json['title'],
summary = json['summary'],
soft = SoftAllBean.fromJson(json['soft']), //对象初始化
steps = (json['steps'] as List).map((e) => StepBean.fromJson(e)).toList(); //list初始化

Map<String, dynamic> toJson() =>
{'title': title, 'summary': summary, 'soft': soft, 'steps': steps};
}