# Model 数据模型
# Dart基本数据类型
- Numbers
- Strings
- Booleans
- List(数组)
- Maps(字典)
# 一层数据结构
data(数据)
{
"code": 200,
"status": "success"
}
1
2
3
4
2
3
4
model(模型)
class FirstLevel {
int code;
String status;
FirstLevel({this.code, this.status});
FirstLevel.fromJson(Map<String, dynamic> json) {
code = json['code'];
status = json['status'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['status'] = this.status;
return data;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 两层数据结构
data(数据)
{
"code": 200,
"status": "success",
"data": [
{
"id": 1,
"name": "老大",
"age": "30",
"height": "178"
},
{
"id": 2,
"name": "老二",
"age": "18",
"height": "175"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
model(模型)
class FirstLevel {
int code;
String status;
List<TwoLevel> data;
FirstLevel({this.code, this.status, this.data});
FirstLevel.fromJson(Map<String, dynamic> json) {
code = json['code'];
status = json['status'];
if (json['data'] != null) {
data = new List<TwoLevel>();
json['data'].forEach((v) {
data.add(new TwoLevel.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['status'] = this.status;
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class TwoLevel {
int id;
String name;
String age;
String height;
TwoLevel({this.id, this.name, this.age, this.height});
TwoLevel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
age = json['age'];
height = json['height'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['age'] = this.age;
data['height'] = this.height;
return data;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 自动生成model结构网站
JSON to Dart (opens new window)