Reputation: 2186
This json response work fine with below List.
[{"id":"1","ename":"KING","sal":"5000"............
List<Emp> emp= (json.decode(response.body) as List)
.map((data) => Emp.fromJson(data))
.toList();
But how in list load data when on start json stay items
{"items":[{"id":1,"ename":"KING","sal":5000............
Upvotes: 1
Views: 310
Reputation: 6186
Use json.decode(response.body)['items']
instead of json.decode(response.body)
An example:
import 'dart:convert';
void main() {
// sample body.response
final j = '''{
"items": [
{"id":"1","ename":"KING","sal":"5000"},
{"id":"2","ename":"KING","sal":"5000"}
]
}''';
List<Emp> emp = (json.decode(j)['items'] as List)
.map((data) => Emp.fromJson(data))
.toList();
print(emp);
}
class Emp {
String id;
String ename;
String sal;
Emp({this.id, this.ename, this.sal});
Emp.fromJson(Map<String, dynamic> json) {
id = json['id'];
ename = json['ename'];
sal = json['sal'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['ename'] = this.ename;
data['sal'] = this.sal;
return data;
}
}
Upvotes: 1
Reputation: 5049
List<Emp> emp= (json.decode(response.body)["items"] as List)
.map((data) => Emp.fromJson(data))
.toList();
Upvotes: 1