Skip to content

Commit ca95ab9

Browse files
authored
Null safety (#33)
* update * migrate to null safety. * migrate to null safety.
1 parent a0fdc24 commit ca95ab9

15 files changed

+299
-243
lines changed

Test/EmptyResp.dart

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import 'dart:convert' show json;
22

33
class EmptyResp {
4-
Qwe qwe;
4+
Qwe? qwe;
55

66
EmptyResp.fromParams({this.qwe});
77

8-
factory EmptyResp(jsonStr) => jsonStr == null
9-
? null
10-
: jsonStr is String
11-
? EmptyResp.fromJson(json.decode(jsonStr))
12-
: EmptyResp.fromJson(jsonStr);
8+
factory EmptyResp(Object jsonStr) => jsonStr is String
9+
? EmptyResp.fromJson(json.decode(jsonStr))
10+
: EmptyResp.fromJson(jsonStr);
11+
12+
static EmptyResp? parse(jsonStr) =>
13+
['null', '', null].contains(jsonStr) ? null : EmptyResp(jsonStr);
1314

1415
EmptyResp.fromJson(jsonRes) {
1516
qwe = jsonRes['qwe'] == null ? null : Qwe.fromJson(jsonRes['qwe']);
@@ -19,37 +20,41 @@ class EmptyResp {
1920
String toString() {
2021
return '{"qwe": $qwe}';
2122
}
23+
24+
String toJSON() => this.toString();
2225
}
2326

2427
class Qwe {
25-
List<dynamic> asd;
26-
List<Object> qaz;
27-
List<dynamic> zxc;
28+
List<dynamic?>? asd;
29+
List<Object?>? qaz;
30+
List<dynamic?>? zxc;
2831

2932
Qwe.fromParams({this.asd, this.qaz, this.zxc});
3033

3134
Qwe.fromJson(jsonRes) {
3235
asd = jsonRes['asd'] == null ? null : [];
3336

3437
for (var asdItem in asd == null ? [] : jsonRes['asd']) {
35-
asd.add(asdItem);
38+
asd!.add(asdItem);
3639
}
3740

3841
qaz = jsonRes['qaz'] == null ? null : [];
3942

4043
for (var qazItem in qaz == null ? [] : jsonRes['qaz']) {
41-
qaz.add(qazItem);
44+
qaz!.add(qazItem);
4245
}
4346

4447
zxc = jsonRes['zxc'] == null ? null : [];
4548

4649
for (var zxcItem in zxc == null ? [] : jsonRes['zxc']) {
47-
zxc.add(zxcItem);
50+
zxc!.add(zxcItem);
4851
}
4952
}
5053

5154
@override
5255
String toString() {
5356
return '{"asd": $asd, "qaz": $qaz, "zxc": $zxc}';
5457
}
58+
59+
String toJSON() => this.toString();
5560
}

Test/IgnoreMapResp.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import 'dart:convert' show json;
22

33
class IgnoreMapResp {
4-
Data data;
4+
Data? data;
55

66
IgnoreMapResp.fromParams({this.data});
77

8-
factory IgnoreMapResp(jsonStr) => jsonStr == null
9-
? null
10-
: jsonStr is String
11-
? IgnoreMapResp.fromJson(json.decode(jsonStr))
12-
: IgnoreMapResp.fromJson(jsonStr);
8+
factory IgnoreMapResp(Object jsonStr) => jsonStr is String
9+
? IgnoreMapResp.fromJson(json.decode(jsonStr))
10+
: IgnoreMapResp.fromJson(jsonStr);
11+
12+
static IgnoreMapResp? parse(jsonStr) =>
13+
['null', '', null].contains(jsonStr) ? null : IgnoreMapResp(jsonStr);
1314

1415
IgnoreMapResp.fromJson(jsonRes) {
1516
data = jsonRes['data'] == null ? null : Data.fromJson(jsonRes['data']);
@@ -19,16 +20,18 @@ class IgnoreMapResp {
1920
String toString() {
2021
return '{"data": $data}';
2122
}
23+
24+
String toJSON() => this.toString();
2225
}
2326

2427
class Data {
25-
int wc;
26-
String author;
27-
String content;
28-
String digest;
29-
String title;
30-
Map<String, dynamic> date;
31-
Extra extra;
28+
int? wc;
29+
String? author;
30+
String? content;
31+
String? digest;
32+
String? title;
33+
Map<String, dynamic>? date;
34+
Extra? extra;
3235

3336
Data.fromParams(
3437
{this.wc,
@@ -53,12 +56,14 @@ class Data {
5356
String toString() {
5457
return '{"wc": $wc, "author": ${author != null ? '${json.encode(author)}' : 'null'}, "content": ${content != null ? '${json.encode(content)}' : 'null'}, "digest": ${digest != null ? '${json.encode(digest)}' : 'null'}, "title": ${title != null ? '${json.encode(title)}' : 'null'}, "date": ${date != null ? '${json.encode(date)}' : 'null'}, "extra": $extra}';
5558
}
59+
60+
String toJSON() => this.toString();
5661
}
5762

5863
class Extra {
59-
int a;
60-
int b;
61-
int c;
64+
int? a;
65+
int? b;
66+
int? c;
6267

6368
Extra.fromParams({this.a, this.b, this.c});
6469

@@ -72,4 +77,6 @@ class Extra {
7277
String toString() {
7378
return '{"a": $a, "b": $b, "c": $c}';
7479
}
80+
81+
String toJSON() => this.toString();
7582
}

Test/ListTopResp.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import 'dart:convert' show json;
22

33
class ListTopResp {
4-
List<Resp> list;
4+
List<Resp?>? list;
55

66
ListTopResp.fromParams({this.list});
77

8-
factory ListTopResp(jsonStr) => jsonStr == null
9-
? null
10-
: jsonStr is String
11-
? ListTopResp.fromJson(json.decode(jsonStr))
12-
: ListTopResp.fromJson(jsonStr);
8+
factory ListTopResp(Object jsonStr) => jsonStr is String
9+
? ListTopResp.fromJson(json.decode(jsonStr))
10+
: ListTopResp.fromJson(jsonStr);
11+
12+
static ListTopResp? parse(jsonStr) =>
13+
['null', '', null].contains(jsonStr) ? null : ListTopResp(jsonStr);
1314

1415
ListTopResp.fromJson(jsonRes) {
1516
list = jsonRes == null ? null : [];
1617

1718
for (var listItem in list == null ? [] : jsonRes) {
18-
list.add(listItem == null ? null : Resp.fromJson(listItem));
19+
list!.add(listItem == null ? null : Resp.fromJson(listItem));
1920
}
2021
}
2122

2223
@override
2324
String toString() {
2425
return '{"json_list": $list}';
2526
}
27+
28+
String toJSON() => this.toString();
2629
}
2730

2831
class Resp {
29-
String a;
30-
String b;
32+
String? a;
33+
String? b;
3134

3235
Resp.fromParams({this.a, this.b});
3336

@@ -40,4 +43,6 @@ class Resp {
4043
String toString() {
4144
return '{"a": ${a != null ? '${json.encode(a)}' : 'null'}, "b": ${b != null ? '${json.encode(b)}' : 'null'}}';
4245
}
46+
47+
String toJSON() => this.toString();
4348
}

Test/ListWithStringResp.dart

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
11
import 'dart:convert' show json;
22

33
class ListWithStringResp {
4-
List<String> asd;
5-
List<List<String>> qaz;
6-
List<Qwe> zxc;
4+
List<String?>? asd;
5+
List<List<String?>?>? qaz;
6+
List<Qwe?>? zxc;
77

88
ListWithStringResp.fromParams({this.asd, this.qaz, this.zxc});
99

10-
factory ListWithStringResp(jsonStr) => jsonStr == null
11-
? null
12-
: jsonStr is String
13-
? ListWithStringResp.fromJson(json.decode(jsonStr))
14-
: ListWithStringResp.fromJson(jsonStr);
10+
factory ListWithStringResp(Object jsonStr) => jsonStr is String
11+
? ListWithStringResp.fromJson(json.decode(jsonStr))
12+
: ListWithStringResp.fromJson(jsonStr);
13+
14+
static ListWithStringResp? parse(jsonStr) =>
15+
['null', '', null].contains(jsonStr) ? null : ListWithStringResp(jsonStr);
1516

1617
ListWithStringResp.fromJson(jsonRes) {
1718
asd = jsonRes['asd'] == null ? null : [];
1819

1920
for (var asdItem in asd == null ? [] : jsonRes['asd']) {
20-
asd.add(asdItem);
21+
asd!.add(asdItem);
2122
}
2223

2324
qaz = jsonRes['qaz'] == null ? null : [];
2425

2526
for (var qazItem in qaz == null ? [] : jsonRes['qaz']) {
26-
List<String> qazChild = qazItem == null ? null : [];
27+
List<String?>? qazChild = qazItem == null ? null : [];
2728
for (var qazItemItem in qazChild == null ? [] : qazItem) {
28-
qazChild.add(qazItemItem);
29+
qazChild!.add(qazItemItem);
2930
}
30-
qaz.add(qazChild);
31+
qaz!.add(qazChild);
3132
}
3233

3334
zxc = jsonRes['zxc'] == null ? null : [];
3435

3536
for (var zxcItem in zxc == null ? [] : jsonRes['zxc']) {
36-
zxc.add(zxcItem == null ? null : Qwe.fromJson(zxcItem));
37+
zxc!.add(zxcItem == null ? null : Qwe.fromJson(zxcItem));
3738
}
3839
}
3940

4041
@override
4142
String toString() {
4243
return '{"asd": ${asd != null ? '${json.encode(asd)}' : 'null'}, "qaz": ${qaz != null ? '${json.encode(qaz)}' : 'null'}, "zxc": $zxc}';
4344
}
45+
46+
String toJSON() => this.toString();
4447
}
4548

4649
class Qwe {
47-
String qwe;
50+
String? qwe;
4851

4952
Qwe.fromParams({this.qwe});
5053

@@ -56,4 +59,6 @@ class Qwe {
5659
String toString() {
5760
return '{"qwe": ${qwe != null ? '${json.encode(qwe)}' : 'null'}}';
5861
}
62+
63+
String toJSON() => this.toString();
5964
}

Test/ListsResp.dart

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
import 'dart:convert' show json;
22

33
class ListsResp {
4-
List<List<List<int>>> asd;
5-
List<int> qaz;
6-
List<List<List<Zxc>>> qwe;
4+
List<List<List<int?>?>?>? asd;
5+
List<int?>? qaz;
6+
List<List<List<Zxc?>?>?>? qwe;
77

88
ListsResp.fromParams({this.asd, this.qaz, this.qwe});
99

10-
factory ListsResp(jsonStr) => jsonStr == null
11-
? null
12-
: jsonStr is String
13-
? ListsResp.fromJson(json.decode(jsonStr))
14-
: ListsResp.fromJson(jsonStr);
10+
factory ListsResp(Object jsonStr) => jsonStr is String
11+
? ListsResp.fromJson(json.decode(jsonStr))
12+
: ListsResp.fromJson(jsonStr);
13+
14+
static ListsResp? parse(jsonStr) =>
15+
['null', '', null].contains(jsonStr) ? null : ListsResp(jsonStr);
1516

1617
ListsResp.fromJson(jsonRes) {
1718
asd = jsonRes['asd'] == null ? null : [];
1819

1920
for (var asdItem in asd == null ? [] : jsonRes['asd']) {
20-
List<List<int>> asdChild = asdItem == null ? null : [];
21+
List<List<int?>?>? asdChild = asdItem == null ? null : [];
2122
for (var asdItemItem in asdChild == null ? [] : asdItem) {
22-
List<int> asdChildChild = asdItemItem == null ? null : [];
23+
List<int?>? asdChildChild = asdItemItem == null ? null : [];
2324
for (var asdItemItemItem in asdChildChild == null ? [] : asdItemItem) {
24-
asdChildChild.add(asdItemItemItem);
25+
asdChildChild!.add(asdItemItemItem);
2526
}
26-
asdChild.add(asdChildChild);
27+
asdChild!.add(asdChildChild);
2728
}
28-
asd.add(asdChild);
29+
asd!.add(asdChild);
2930
}
3031

3132
qaz = jsonRes['qaz'] == null ? null : [];
3233

3334
for (var qazItem in qaz == null ? [] : jsonRes['qaz']) {
34-
qaz.add(qazItem);
35+
qaz!.add(qazItem);
3536
}
3637

3738
qwe = jsonRes['qwe'] == null ? null : [];
3839

3940
for (var qweItem in qwe == null ? [] : jsonRes['qwe']) {
40-
List<List<Zxc>> qweChild = qweItem == null ? null : [];
41+
List<List<Zxc?>?>? qweChild = qweItem == null ? null : [];
4142
for (var qweItemItem in qweChild == null ? [] : qweItem) {
42-
List<Zxc> qweChildChild = qweItemItem == null ? null : [];
43+
List<Zxc?>? qweChildChild = qweItemItem == null ? null : [];
4344
for (var qweItemItemItem in qweChildChild == null ? [] : qweItemItem) {
44-
qweChildChild.add(
45+
qweChildChild!.add(
4546
qweItemItemItem == null ? null : Zxc.fromJson(qweItemItemItem));
4647
}
47-
qweChild.add(qweChildChild);
48+
qweChild!.add(qweChildChild);
4849
}
49-
qwe.add(qweChild);
50+
qwe!.add(qweChild);
5051
}
5152
}
5253

5354
@override
5455
String toString() {
5556
return '{"asd": $asd, "qaz": $qaz, "qwe": $qwe}';
5657
}
58+
59+
String toJSON() => this.toString();
5760
}
5861

5962
class Zxc {
60-
int zxc;
63+
int? zxc;
6164

6265
Zxc.fromParams({this.zxc});
6366

@@ -69,4 +72,6 @@ class Zxc {
6972
String toString() {
7073
return '{"zxc": $zxc}';
7174
}
75+
76+
String toJSON() => this.toString();
7277
}

0 commit comments

Comments
 (0)