如何解决json检索迭代器键列表中的flutter错误
在我的json响应中,我试图检索动态键值,并根据键需要解析元素,以下是一个示例json响应
{
"success": true,"data": {
"ones": [{
"id": "2","username": "LM10002"
},{
"id": "6","username": "LM10006"
}
],"twos": [],"threes": [{
"id": "4","username": "LM10004"
}],"fours": [{
"id": "5","username": "LM10005"
},{
"id": "14","username": "GT10014"
}
]
}
}
以下是我检索关键值的代码
@override
void initState() {
super.initState();
getData();
}
getData() {
Map mapValue = jsonDecode(json);
// This is where you iterate via the data object
// that is the value which is key,value pair
List<Data> data = List();
mapValue['data'].forEach((key,value) {
List<User> user = List();
value.forEach((item) {
user.add(User(id: item['id'],username: item['username']));
});
data.add(Data(name: key,userList: user));
});
data.forEach((element) {
print(element.name + " : " + '${element.userList.length}');
});
}
}
class Data {
final String name;
final List<User> userList;
Data({this.name,this.userList});
}
class User {
final String id;
final String username;
User({this.id,this.username});
}
在这里,我能够获得诸如“一”,“二”之类的关键术语,并且将其显示在列表中(直到现在,我的代码才能正常工作),当用户单击列表时,我需要检索这些键的值并在视图中显示。基于键的方式/位置检索键列表值的方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。