3

I fetched the JSON data in flutter application, I am attempting to display it PageView.builder and nested ListView.builder, this model I have already created.

The JSON data is configured like this first Order Details and then it has nested order_items. I need to display the single order in one Page and then the order_items on the same page in ListView.Builder.

In this attempt I faced the below error

ERROR

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

And I am not able to index the elements for the for pageview and nested ListView.builder for order_items.

Please guide me how should I correct it

This is the JSON data which I am able to fetch from the server in flutter application

{
    "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",
                },
               
            ]
        }
    ]
}

The code I used for it is this for fetching the data in the 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);
 }
    

  }

This is the code I am using to fetch the field data

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

WIDGET CODE that is being implemented in the flutter application

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,

                                                   ),),
                                                 ),
                                               ),

                                             ],),
],
                                       ),
                                     ],
                                   );
                                 },
                               ),
                             )
                           ],
                         );
                       });
                 }
             }
           })
  • The error suggests that there is a mismatch of type. Maybe you are assigning a List where fultter expects a Map. Refer to the stack trace of the error to find at what point the error occurs. – Afridi Kayal Aug 29 '20 at 05:03
  • @AfridiKayal in the logcat it is not giving error details, the error is shown from the screen upon testing. I am new to this learning, please guide me further, if anymore code information is required do let me know –  Aug 29 '20 at 05:24
  • Can you perhaps add the Widget code? If the error is shown on the screen with a red background, it is possible that the problem occurred while building the widget. – Afridi Kayal Aug 29 '20 at 08:38
  • @AfridiKayal I have added the widget code, please check –  Aug 29 '20 at 09:02

0 Answers0