I am able to fetch the JSON data from the server in the flutter application. I need to display the data in PageView.builder and nested ListView.builder, this model I have already created.
The code called for implementation
orderdetail = NewOrder.fromJson(json.decode(response.body));
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",
"order_items": [
{
"comp_code": "4",
"comp_name": "KMT OVERSEAS",
"order_no": "16",
},
{
"comp_code": "4",
"comp_name": "KMT OVERSEAS",
"order_no": "16",
}
]
},
{
"comp_code": "4",
"comp_name": "KMT OVERSEAS",
"order_no": "18",
"order_items": [
{
"comp_code": "4",
"comp_name": "KMT OVERSEAS",
"order_no": "18",
},
{
"comp_code": "4",
"comp_name": "KMT OVERSEAS",
"order_no": "18",
},
{
"comp_code": "4",
"comp_name": "KMT OVERSEAS",
"order_no": "18",
},
]
}
]
}
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
};
var response = await http.post(newapi, body: data);
if(response.statusCode == 200) {
jsonResponse = json.decode(response.body);
print("jsonrespnse");
print(jsonResponse);
}
} else {
setState(() {
_isLoading = false;
});
print('login error');
print(response.body);
}
}
The NewOrder Model class I am implemented is below
import 'newitem.dart';
class NewOrder {
NewOrder({
this.sohPk,
this.orderNo,
this.newItem,
});
String sohPk;
String orderNo;
NewItem newItem;
factory NewOrder.fromJson(Map<String, dynamic> json) => NewOrder(
sohPk: json["soh_pk"],
orderNo: json["order_no"],
newItem:NewItem.fromJson(json['order_items'])
);
Map<String, dynamic> toJson() => {
"soh_pk": sohPk,
"order_no": orderNo,
'order_items': newItem.toJson()
};
}
The NewPlayLoadClass I Implemented is here
import 'package:dataproject2/newmodel/neworder.dart';
class NewPayLoad {
String error;
List<NewOrder> content;
NewPayLoad({this.error, this.content});
NewPayLoad.fromJson(Map<String, dynamic> json) {
error = json['error'];
if (json['content'] != null) {
content = new List<NewOrder>();
json['content'].forEach((v) {
content.add(new NewOrder.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;
}
}
The error I am getting is here
The following NoSuchMethodError was thrown building PageViewClass(dirty, state: _PageViewClassState#98a84): The getter 'content' was called on null. Receiver: null Tried calling: content
I am not able to understand what is wrong with my code, and how to resolve it Please guide further