在flutter中为JSON数据配置字段时遇到错误

问题描述

我在flutter应用程序中获取了JSON数据,我试图将其显示为PageView.builder和嵌套的ListView.builder,这是我已经创建的模型。

首先像这样配置JSON数据订单详细信息,然后它具有嵌套的order_items。我需要在一页中显示单个订单,然后在ListView.Builder中的同一页面上显示order_items。

在这种尝试中,我遇到了以下错误

错误

TypeList<dynamic> is not a subtype of Map<dynamic,dynamic>

我无法为pageview的元素编制索引,而为order_items嵌套的ListView.builder编制索引。

请指导我如何纠正它

这是我可以在flutter应用程序中从服务器获取的JSON数据

{
    "error": "false","content": [
        {
            "comp_code": "4","comp_name": "KMT OVERSEAS","order_no": "16","soh_pk": "23660","order_items": [
                {
                    "comp_code": "4","sod_pk": "31689",},{
                    "comp_code": "4","sod_pk": "31688",}
            ]
        },{
            "comp_code": "4","order_no": "18","soh_pk": "23702","sod_pk": "31749","sod_pk": "31742","sod_pk": "31743",]
        }
    ]
}

我使用的代码是用于获取有状态小部件中数据的

Future<Payload> getdetailsoforders(String userid,String companycode) async {

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
      'user_id': userid,'company_code':companycode

    };

     //newapi is the url link for the server
    var response = await http.post(newapi,body: data);
    if(response.statusCode == 200) {
     jsonResponse = json.decode(response.body);
      print("jsonrespnse");
      print(jsonResponse);
 }
    

  }

这是我用来获取字段数据的代码

NewDetail.fromJson(Map<String,dynamic> json) {
    error = json['error'];
    if (json['content'] != null) {
      content = new List<NewOrderModel>();
      json['content'].forEach((v) {
        content.add(new NewOrderModel.fromJson(v));
      });
    }
  }

  Map<String,dynamic> toJson() {
    final Map<String,dynamic> data = new Map<String,dynamic>();
    data['error'] = this.error;
    if (this.content != null) {
      data['content'] = this.content.map((v) => v.toJson()).toList();
    }
    return data;
  }

在flutter应用程序中正在实现的小工具代码

FutureBuilder(
           future: _future,           builder: (context,AsyncSnapshot<Payload> snapshot) {
             switch (snapshot.connectionState) {
               case ConnectionState.none:
                 return Text('none');
               case ConnectionState.waiting:
                 return Center(child: CircularProgressIndicator());
               case ConnectionState.active:
                 return Text('');
               case ConnectionState.done:
                 if (snapshot.hasError) {
                   return Text(
                     '${snapshot.error}',                     style: TextStyle(color: Colors.red),                   );
                 } else {
  return PageView.builder(
                       scrollDirection: Axis.horizontal,                       itemCount: snapshot.data.content.keys.length,                       itemBuilder: (context,index) {
                         String key =
                         snapshot.data.content.keys.elementAt(index);
                 return Column(
                           children: [
                             SizedBox(height: 25,),                             Column(
                               mainAxisAlignment: MainAxisAlignment.center,                               crossAxisAlignment:CrossAxisAlignment.center,                               children: <Widget>[
                                 Row(
                                   mainAxisAlignment: MainAxisAlignment.center,                                   crossAxisAlignment:CrossAxisAlignment.center,                                   children: <Widget>[
                                     Container(

                                       height: 32,                                       width: 160,                                       decoration: BoxDecoration(
                                         color: Colors.white,                                         border: Border.all(

                                           width:0.5,                                           color: Color(0xFF766F6F),                                         ),                                         borderRadius: BorderRadius.circular(10.0),                                       ),                                       child: Center(
                                         child: Text('Order No.',style: TextStyle(backgroundColor: Colors.white,                                           color:Color(0xFF2e2a2a),                                           fontFamily: 'Roboto',                                           fontSize: 12,                                     ),                                     Container(
                                       height: 32,                                         border: Border.all(
                                           width:0.5,                                       child: Center(
                                         child: Text(key,                                     )]
                                   ,                               ],                             SizedBox(height: 30,                             Expanded(

                               child: ListView.separated(
                                 separatorBuilder:
                                     (BuildContext context,int index) {
                                   return SizedBox(
                                     height: 16,                                   );
                                 },                                 shrinkWrap: true,                                 itemCount: snapshot.data.content[key].length,                                 itemBuilder: (context,index) {
                                  return Column(
                                     children: [

                                       Column(
                                         mainAxisAlignment: MainAxisAlignment.center,                                         crossAxisAlignment: CrossAxisAlignment.center,                                         children: <Widget>[
                                           Row(
                                             mainAxisAlignment: MainAxisAlignment.center,                                             // crossAxisAlignment:CrossAxisAlignment.center,                                             children: <Widget>[
                                               Container(

                                                 height: 32,                                                 width: 160,                                                 decoration: BoxDecoration(
                                                   color: Colors.white,                                                   border: Border.all(

                                                     width:0.5,                                                     color: Color(0xFF766F6F),                                                   ),                                                   borderRadius: BorderRadius.circular(10.0),                                                 ),                                                 child: Center(
                                                   child: Text("Catalog Item",   style: TextStyle(backgroundColor: Colors.white,                                                     color:Color(0xFF2e2a2a),                                                     fontFamily: 'Roboto',                                                     fontSize: 12,                                               ),                                               Container(
                                                 height: 32,                                                 child: Center(
                                                   child: Text(snapshot
                                                       .data.content[key][index][0].sqdFk,                                             ],                                           Row(
                                             mainAxisAlignment: MainAxisAlignment.center,                                             crossAxisAlignment:CrossAxisAlignment.center,                                                   border: Border.all(
                                                     width:0.5,                                                 child: Center(
                                                   child: Text('QTY.',                                                 child: Center(
                                                   child: Text(snapshot
                                                       .data.content[key][index][0].sohFk,],                                     ],                               ),                             )
                           ],                         );
                       });
                 }
             }
           })

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...