【问题标题】:Parse Complex JSON in Flutter在 Flutter 中解析复杂的 JSON
【发布时间】:2020-12-21 07:05:11
【问题描述】:

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

JSON 数据

  {

"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"

                 }
            ]

    }

}

代码

 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),
              );
            } ),
        ),
      );
  }
}

【问题讨论】:

  • 去掉方括号:Map i in newData[1]

标签: json flutter dart


【解决方案1】:

正如埃尔达在评论中所说:

for(Map i in newData[1]){

需要

for(Map i in newData){

【讨论】:

    猜你喜欢
    • 2020-04-13
    • 2020-10-29
    • 2021-06-20
    • 2020-10-21
    • 1970-01-01
    • 2021-07-20
    • 2020-12-20
    相关资源
    最近更新 更多