Skip to content

Commit 91cd921

Browse files
committed
updated json modal with null handling
1 parent 7a31e07 commit 91cd921

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

day_14_01_mvvm_articles/lib/models/article.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ class Article {
77

88
factory Article.fromJSON(Map<String, dynamic> json) {
99
return Article(
10-
title: json['title'],
11-
description: json['description'],
12-
imageURL: json['urlToImage'],
13-
articleSourceURL: json['url'],
10+
title: json['title'] ?? 'No Title Found',
11+
description: json['description'] ?? 'No Description Found',
12+
imageURL: json['urlToImage'] ?? 'https://via.placeholder.com/150',
13+
articleSourceURL: json['url'] ??
14+
'https://via.placeholder.com/468x60?text=visite+article.com+now',
1415
);
1516
}
1617
}

day_14_01_mvvm_star_wars_movie/lib/models/character.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class Character {
1111
{this.name, this.gender, this.birthYear, this.eyeColor, this.height});
1212

1313
factory Character.fromJson(Map<String, dynamic> json) => Character(
14-
name: json['name'],
15-
gender: json['gender'],
16-
birthYear: json['birthYear'],
17-
eyeColor: json['eye_color'],
18-
height: int.parse(json['height']),
14+
name: json['name'] ?? 'No Name Found',
15+
gender: json['gender'] ?? 'No Gender Found',
16+
birthYear: json['birthYear'] ?? 'No BirthYear Found',
17+
eyeColor: json['eye_color'] ?? 'No Eye Color Found',
18+
height: int.parse(json['height']) ?? 0,
1919
);
2020
}

day_14_01_mvvm_star_wars_movie/lib/models/film.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class Film {
2222
});
2323

2424
factory Film.fromJson(Map<String, dynamic> json) => Film(
25-
title: json["title"],
26-
openingCrawl: json["opening_crawl"],
27-
director: json["director"],
28-
producer: json["producer"],
29-
releaseDate: DateTime.parse(json["release_date"]),
25+
title: json["title"] ?? 'No Title Found',
26+
openingCrawl: json["opening_crawl"] ?? 'No Crawl Found',
27+
director: json["director"] ?? 'No Director Name Found',
28+
producer: json["producer"] ?? 'No Producer Name Found',
29+
releaseDate: DateTime.parse(json["release_date"]) ?? DateTime.now(),
3030
);
3131
}

day_14_01_mvvm_star_wars_movie/lib/models/planet.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class Planet {
1616
});
1717

1818
factory Planet.fromJson(Map<String, dynamic> json) => Planet(
19-
name: json['name'],
20-
climate: json['climate'],
21-
terrain: json['terrain'],
22-
gravity: json['gravity'],
23-
population: json['population'],
24-
diameter: int.parse(json['diameter']),
19+
name: json['name'] ?? 'No Name Found',
20+
climate: json['climate'] ?? 'No Climate Found',
21+
terrain: json['terrain'] ?? 'No Terrain Found',
22+
gravity: json['gravity'] ?? 'No Gravity Found',
23+
population: json['population'] ?? 'No Poppulation Found',
24+
diameter: int.parse(json['diameter']) ?? 0,
2525
);
2626
}

0 commit comments

Comments
 (0)