gpt4 book ai didi

json - 在Flutter中解析复杂的JSON

转载 作者:行者123 更新时间:2023-12-03 04:38:22 25 4
gpt4 key购买 nike

我正在将复杂的json数据解析到我的flutter应用程序中。我在下面附加了我的代码和json数据。我将数据放入“newData”中。但是出现以下错误。
错误:“未处理的异常:类型'_InternalLinkedHashMap '不是类型'Iterable'的子类型。想要在Listview中显示它。Help非常感谢

JSON Data

  {

"Status":200,

"Message":"Success",

"data":{

"TotalRecords":10,

"Records":[

{

"Id":1,

"title":"Smile Crowdfunding",

"shortDescription":"This foundation will bring smile on there faces",

"collectedValue":500,

"totalValue":5000,

"startDate":"05/05/2018",

"endDate":"10/06/2018",

"mainImageURL":"http://iphonedeveloperguide.com/oneinr/project1.jpg"

},

{

"Id":2,

"title":"Animal Funding",

"shortDescription":"This foundation will help animals",

"collectedValue":200,

"totalValue":10000,

"startDate":"10/05/2018",

"endDate":"11/06/2018",

"mainImageURL":"http://iphonedeveloperguide.com/oneinr/project2.jpg"

}
]

}

}

Code

 class _ShowListState extends State<ShowList> {
var loading = false;
List<Record> dataModel = [];
Future<Null> getData() async{
final responseData = await http.get("https://xxxxxxx.json");
if(responseData.statusCode == 200){
Map<String, dynamic> map = jsonDecode(responseData.body);
LinkedHashMap<String,dynamic> data = map["data"];
List<dynamic> newData = data["Records"];
print("newData: $newData");
setState(() {
for(Map i in newData[1]){
dataModel.add(Record.fromJson(i));
print("newData: $data");
}
loading = false;
});
}
}
void initState() {
// TODO: implement initState
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Machine Test'),
),
body: Container(
child: loading? Center(
child: CircularProgressIndicator()):
ListView.builder(
itemCount: dataModel.length,
itemBuilder: (context,i){
final nDataList = dataModel[i];
return Container(
child: Text(nDataList.title),
);
} ),
),
);
}
}

最佳答案

正如Eldar在评论中所说:

for(Map i in newData[1]){
需要是
for(Map i in newData){

关于json - 在Flutter中解析复杂的JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63699981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com