-
Notifications
You must be signed in to change notification settings - Fork 298
/
post_model.dart
59 lines (45 loc) · 1.04 KB
/
post_model.dart
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
54
55
56
57
58
59
// Created using https://app.quicktype.io/
// To parse this JSON data, do
//
// final post = postFromJson(jsonString);
import 'dart:convert';
Post postFromJson(String str) {
final jsonData = json.decode(str);
return Post.fromJson(jsonData);
}
String postToJson(Post data) {
final dyn = data.toJson();
return json.encode(dyn);
}
List<Post> allPostsFromJson(String str) {
final jsonData = json.decode(str);
return new List<Post>.from(jsonData.map((x) => Post.fromJson(x)));
}
String allPostsToJson(List<Post> data) {
final dyn = new List<dynamic>.from(data.map((x) => x.toJson()));
return json.encode(dyn);
}
class Post {
int userId;
int id;
String title;
String body;
Post({
this.userId,
this.id,
this.title,
this.body,
});
factory Post.fromJson(Map<String, dynamic> json) => new Post(
userId: json["userId"],
id: json["id"],
title: json["title"],
body: json["body"],
);
Map<String, dynamic> toJson() => {
"userId": userId,
"id": id,
"title": title,
"body": body,
};
}