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