/
dynamic_key_json.dart
81 lines (74 loc) · 2.17 KB
/
dynamic_key_json.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Json sample
{
"91": {
"email": "george.bluth@reqres.in",
"first_name": "George"
},
"22": {
"email": "janet.weaver@reqres.in",
"first_name": "Janet"
},
"31": {
"email": "emma.wong@reqres.in",
"first_name": "Emma"
},
"34": {
"email": "eve.holt@reqres.in",
"first_name": "Eve"
},
"53": {
"email": "charles.morris@reqres.in",
"first_name": "Charles"
},
"61": {
"email": "tracey.ramos@reqres.in",
"first_name": "Tracey"
},
"47": {
"email": "michael.lawson@reqres.in",
"first_name": "Michael"
},
"28": {
"email": "lindsay.ferguson@reqres.in",
"first_name": "Lindsay"
}
}
*/
import 'dart:convert';
import 'package:flutter/material.dart';
String jsonData =
'{ "91": { "email": "george.bluth@reqres.in", "first_name": "George" }, "22": { "email": "janet.weaver@reqres.in", "first_name": "Janet" }, "31": { "email": "emma.wong@reqres.in", "first_name": "Emma" }, "34": { "email": "eve.holt@reqres.in", "first_name": "Eve" }, "53": { "email": "charles.morris@reqres.in", "first_name": "Charles" }, "61": { "email": "tracey.ramos@reqres.in", "first_name": "Tracey" }, "47": { "email": "michael.lawson@reqres.in", "first_name": "Michael" }, "28": { "email": "lindsay.ferguson@reqres.in", "first_name": "Lindsay" } }';
class User {
final String id;
final String name;
final String email;
User(this.id, this.name, this.email);
static List<User> parseList(Map<String, dynamic> map) {
List<User> users = [];
map.forEach((key, value) {
users.add(User(key, value["first_name"], value["email"]));
});
return users;
}
}
class DynamicKeyJsonExampleScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dynamic Key Json"),
),
body: ListView(
children: User.parseList(json.decode(jsonData))
.map((i) => Card(
child: ListTile(
title: Text(i.name),
subtitle: Text(i.email),
leading: CircleAvatar(child: Text(i.id)),
),
))
.toList(),
),
);
}
}