I'm using json_model plugin for making PODO class. I've successfully parsed the main array. But I can't put the images key that contains array of map in PODO class using that plugin and can't parse the data.
Here is the array that I want to parse below:
[
{
"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"
}
]
},
...
]
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 data from images key:
FutureBuilder<List<Users>>(
future: _fetchUser(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (snapshot.hasData) {
List<Users> users = snapshot.data;
return ListView.separated(
separatorBuilder: (_, __) => const Divider(),
itemCount: users.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${users[index].username}'),
subtitle: Text('${users[index].images[index].imageName}'),
onTap: () {
Navigator.pushNamed(
context,
DetailsScreen.route,
arguments: users[index],
);
},
);
},
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
But it's showing the error:
Index out of range: index should be less than 5:5