【发布时间】:2019-12-30 20:17:38
【问题描述】:
我正在使用 json_model 插件来制作 PODO 类。我已经成功解析了主数组。但是我无法使用该插件将包含 Map 数组的“图像”键放入 PODO 类中,并且无法解析数据。
这是我要解析的数组。
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"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"
}
]
},
...
]
谢谢!!!
这里解析json数据
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!');
}
}
正在尝试显示“图像”数据...
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());
}
},
)
但它显示错误:索引超出范围:索引应小于 5:5
【问题讨论】: