3

我在颤振应用程序中获取了 JSON 数据,我试图显示它 PageView.builder 和嵌套的 ListView.builder,我已经创建了这个模型。

JSON 数据是这样配置的,首先是 Order Details,然后是嵌套的 order_items。我需要在一个页面中显示单个订单,然后在 ListView.Builder 的同一页面上显示 order_items。

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

错误

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

而且我无法为 pageview 的元素和 order_items 的嵌套 ListView.builder 索引。

请指导我应该如何纠正它

这是我能够在颤振应用程序中从服务器获取的 JSON 数据

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

                },
                {
                    "comp_code": "4",
                    "comp_name": "KMT OVERSEAS",
                    "order_no": "18",
                    "sod_pk": "31743",
                },
               
            ]
        }
    ]
}

我使用的代码是用于在 Stateful Widget 中获取数据的

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 应用程序中正在实现的 WIDGET CODE

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,
                                       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(key, style: TextStyle(backgroundColor: Colors.white,
                                           color:Color(0xFF2e2a2a),
                                           fontFamily: 'Roboto',
                                           fontSize: 12,

                                         ),
                                         ),
                                       ),

                                     )]
                                   ,),
                               ],),
                             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,
                                                 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(snapshot
                                                       .data.content[key][index][0].sqdFk,   style: TextStyle(backgroundColor: Colors.white,
                                                     color:Color(0xFF2e2a2a),
                                                     fontFamily: 'Roboto',
                                                     fontSize: 12,

                                                   ),
                                                   ),
                                                 ),
                                               ),

                                             ],),
                                           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('QTY.',   style: TextStyle(backgroundColor: Colors.white,
                                                     color:Color(0xFF2e2a2a),
                                                     fontFamily: 'Roboto',
                                                     fontSize: 12,

                                                   ),
                                                   ),
                                                 ),
                                               ),
                                               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(snapshot
                                                       .data.content[key][index][0].sohFk, style: TextStyle(backgroundColor: Colors.white,
                                                     color:Color(0xFF2e2a2a),
                                                     fontFamily: 'Roboto',
                                                     fontSize: 12,

                                                   ),),
                                                 ),
                                               ),

                                             ],),
],
                                       ),
                                     ],
                                   );
                                 },
                               ),
                             )
                           ],
                         );
                       });
                 }
             }
           })
4

0 回答 0