Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 2cae46b

Browse files
committed
Merge pull request #1 from chalin/patch-0305-json
Fix `toJsonString()`
2 parents a478731 + ec53984 commit 2cae46b

File tree

3 files changed

+36
-57
lines changed

3 files changed

+36
-57
lines changed

Chapter_05/lib/recipe.dart

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
library recipe;
22

3-
import 'dart:convert';
4-
53
class Recipe {
64
String id;
75
String name;
@@ -14,22 +12,17 @@ class Recipe {
1412
Recipe(this.id, this.name, this.category, this.ingredients, this.directions,
1513
this.rating, this.imgUrl);
1614

17-
String toJsonString() {
18-
Map data = {
19-
"id" : id,
20-
"name" : name,
21-
"category" : category,
22-
"ingredients" : ingredients,
23-
"directions" : directions,
24-
"rating" : rating,
25-
"imgUrl" : imgUrl
26-
};
27-
return JSON.encode(data);
28-
}
15+
Map<String, dynamic> toJson() => <String, dynamic>{
16+
"id": id,
17+
"name": name,
18+
"category": category,
19+
"ingredients": ingredients,
20+
"directions": directions,
21+
"rating": rating,
22+
"imgUrl": imgUrl
23+
};
2924

30-
factory Recipe.fromJsonMap(Map json) {
31-
return new Recipe(json['id'], json['name'], json['category'],
32-
json['ingredients'], json['directions'], json['rating'],
33-
json['imgUrl']);
34-
}
25+
factory Recipe.fromJsonMap(Map<String, dynamic> json) => new Recipe(json['id'], json['name'],
26+
json['category'], json['ingredients'], json['directions'], json['rating'],
27+
json['imgUrl']);
3528
}

Chapter_06/lib/service/recipe.dart

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
library recipe;
22

3-
import 'dart:convert';
4-
53
class Recipe {
64
String id;
75
String name;
@@ -14,22 +12,17 @@ class Recipe {
1412
Recipe(this.id, this.name, this.category, this.ingredients, this.directions,
1513
this.rating, this.imgUrl);
1614

17-
String toJsonString() {
18-
Map data = {
19-
"id" : id,
20-
"name" : name,
21-
"category" : category,
22-
"ingredients" : ingredients,
23-
"directions" : directions,
24-
"rating" : rating,
25-
"imgUrl" : imgUrl
26-
};
27-
return JSON.encode(data);
28-
}
15+
Map<String, dynamic> toJson() => <String, dynamic>{
16+
"id": id,
17+
"name": name,
18+
"category": category,
19+
"ingredients": ingredients,
20+
"directions": directions,
21+
"rating": rating,
22+
"imgUrl": imgUrl
23+
};
2924

30-
factory Recipe.fromJsonMap(Map json) {
31-
return new Recipe(json['id'], json['name'], json['category'],
32-
json['ingredients'], json['directions'], json['rating'],
33-
json['imgUrl']);
34-
}
25+
factory Recipe.fromJsonMap(Map<String, dynamic> json) => new Recipe(json['id'], json['name'],
26+
json['category'], json['ingredients'], json['directions'], json['rating'],
27+
json['imgUrl']);
3528
}

Chapter_07/lib/service/recipe.dart

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
library recipe;
22

3-
import 'dart:convert';
4-
53
class Recipe {
64
String id;
75
String name;
@@ -14,22 +12,17 @@ class Recipe {
1412
Recipe(this.id, this.name, this.category, this.ingredients, this.directions,
1513
this.rating, this.imgUrl);
1614

17-
String toJsonString() {
18-
Map data = {
19-
"id" : id,
20-
"name" : name,
21-
"category" : category,
22-
"ingredients" : ingredients,
23-
"directions" : directions,
24-
"rating" : rating,
25-
"imgUrl" : imgUrl
26-
};
27-
return JSON.encode(data);
28-
}
15+
Map<String, dynamic> toJson() => <String, dynamic>{
16+
"id": id,
17+
"name": name,
18+
"category": category,
19+
"ingredients": ingredients,
20+
"directions": directions,
21+
"rating": rating,
22+
"imgUrl": imgUrl
23+
};
2924

30-
factory Recipe.fromJsonMap(Map json) {
31-
return new Recipe(json['id'], json['name'], json['category'],
32-
json['ingredients'], json['directions'], json['rating'],
33-
json['imgUrl']);
34-
}
25+
factory Recipe.fromJsonMap(Map<String, dynamic> json) => new Recipe(json['id'], json['name'],
26+
json['category'], json['ingredients'], json['directions'], json['rating'],
27+
json['imgUrl']);
3528
}

0 commit comments

Comments
 (0)