I'm using json_model plugin for making PODO class. I've successfully parsed main Array. But I can't put "images" key that contains Array of Map in PODO class using that plugin and can't parsing the data.
Here is the Array that I want to parse.
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
},
"images": [
{
"id": 11,
"imageName": "xCh-rhy"
},
{
"id": 31,
"imageName": "fjs-eun"
},
{
"id": 51,
"imageName": "asd-fdg"
},
{
"id": 71,
"imageName": "zxc-cvb"
},
{
"id": 91,
"imageName": "qwe-hgj"
}
]
},
...
]
Thanks!!!
Parsing json data here
Future<List<Users>> _fetchUser() async {
final response =
await DefaultAssetBundle.of(context).loadString('users.json');
if (response != null) {
List users = json.decode(response.toString());
return users.map((user) => Users.fromJson(user)).toList();
} else {
throw Exception('Failed to load data!');
}
}
Trying to show "images" data...
FutureBuilder<List<Users>>(
future: _fetchUser(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (snapshot.hasData) {
List<Users> users = snapshot.data;
return ListView.separated(
itemCount: users.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('${users[index].username}'),
subtitle: Text('${users[index].images[index].imageName}'),
onTap: () {
Navigator.pushNamed(context, DetailsScreen.route,
arguments: users[index]);
},
);
},
separatorBuilder: (BuildContext context, int index) =>
Divider(),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
)
But it's showing the error : Index out of range: index should be less than 5:5