无法将数据从 JSON 获取到 flutter 中的字段
Not able to get the data from the JSON to fields in flutter
我能够在 flutter 应用程序中从服务器获取 JSON 数据。我需要在 PageView.builder 和嵌套 ListView.builder 中显示数据,这个模型我已经创建了。
调用实现的代码
orderdetail = NewOrder.fromJson(json.decode(response.body));
这是我能够在 flutter 应用程序中从服务器获取的 JSON 数据
{
"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",
},
]
}
]
}
我使用的代码是用来获取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);
}
}
我实现的NewOrder模型class如下
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()
};
}
我实现的NewPlayLoadClass在这里
进口'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 following NoSuchMethodError was thrown building
PageViewClass(dirty, state: _PageViewClassState#98a84): The getter
'content' was called on null. Receiver: null Tried calling: content
我无法理解我的代码有什么问题,以及如何解决它
请进一步指导
使用下面的代码
class NewOrder {
String _error;
List<Content> _content;
NewOrder({String error, List<Content> content}) {
this._error = error;
this._content = content;
}
String get error => _error;
set error(String error) => _error = error;
List<Content> get content => _content;
set content(List<Content> content) => _content = content;
NewOrder.fromJson(Map<String, dynamic> json) {
_error = json['error'];
if (json['content'] != null) {
_content = new List<Content>();
json['content'].forEach((v) {
_content.add(new Content.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;
}
}
class Content {
String _compCode;
String _compName;
String _orderNo;
List<OrderItems> _orderItems;
Content(
{String compCode,
String compName,
String orderNo,
List<OrderItems> orderItems}) {
this._compCode = compCode;
this._compName = compName;
this._orderNo = orderNo;
this._orderItems = orderItems;
}
String get compCode => _compCode;
set compCode(String compCode) => _compCode = compCode;
String get compName => _compName;
set compName(String compName) => _compName = compName;
String get orderNo => _orderNo;
set orderNo(String orderNo) => _orderNo = orderNo;
List<OrderItems> get orderItems => _orderItems;
set orderItems(List<OrderItems> orderItems) => _orderItems = orderItems;
Content.fromJson(Map<String, dynamic> json) {
_compCode = json['comp_code'];
_compName = json['comp_name'];
_orderNo = json['order_no'];
if (json['order_items'] != null) {
_orderItems = new List<OrderItems>();
json['order_items'].forEach((v) {
_orderItems.add(new OrderItems.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['comp_code'] = this._compCode;
data['comp_name'] = this._compName;
data['order_no'] = this._orderNo;
if (this._orderItems != null) {
data['order_items'] = this._orderItems.map((v) => v.toJson()).toList();
}
return data;
}
}
class OrderItems {
String _compCode;
String _compName;
String _orderNo;
OrderItems({String compCode, String compName, String orderNo}) {
this._compCode = compCode;
this._compName = compName;
this._orderNo = orderNo;
}
String get compCode => _compCode;
set compCode(String compCode) => _compCode = compCode;
String get compName => _compName;
set compName(String compName) => _compName = compName;
String get orderNo => _orderNo;
set orderNo(String orderNo) => _orderNo = orderNo;
OrderItems.fromJson(Map<String, dynamic> json) {
_compCode = json['comp_code'];
_compName = json['comp_name'];
_orderNo = json['order_no'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['comp_code'] = this._compCode;
data['comp_name'] = this._compName;
data['order_no'] = this._orderNo;
return data;
}
}
我能够在 flutter 应用程序中从服务器获取 JSON 数据。我需要在 PageView.builder 和嵌套 ListView.builder 中显示数据,这个模型我已经创建了。
调用实现的代码
orderdetail = NewOrder.fromJson(json.decode(response.body));
这是我能够在 flutter 应用程序中从服务器获取的 JSON 数据
{
"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",
},
]
}
]
}
我使用的代码是用来获取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);
}
}
我实现的NewOrder模型class如下
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()
};
}
我实现的NewPlayLoadClass在这里
进口'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 following NoSuchMethodError was thrown building PageViewClass(dirty, state: _PageViewClassState#98a84): The getter 'content' was called on null. Receiver: null Tried calling: content
我无法理解我的代码有什么问题,以及如何解决它 请进一步指导
使用下面的代码
class NewOrder {
String _error;
List<Content> _content;
NewOrder({String error, List<Content> content}) {
this._error = error;
this._content = content;
}
String get error => _error;
set error(String error) => _error = error;
List<Content> get content => _content;
set content(List<Content> content) => _content = content;
NewOrder.fromJson(Map<String, dynamic> json) {
_error = json['error'];
if (json['content'] != null) {
_content = new List<Content>();
json['content'].forEach((v) {
_content.add(new Content.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;
}
}
class Content {
String _compCode;
String _compName;
String _orderNo;
List<OrderItems> _orderItems;
Content(
{String compCode,
String compName,
String orderNo,
List<OrderItems> orderItems}) {
this._compCode = compCode;
this._compName = compName;
this._orderNo = orderNo;
this._orderItems = orderItems;
}
String get compCode => _compCode;
set compCode(String compCode) => _compCode = compCode;
String get compName => _compName;
set compName(String compName) => _compName = compName;
String get orderNo => _orderNo;
set orderNo(String orderNo) => _orderNo = orderNo;
List<OrderItems> get orderItems => _orderItems;
set orderItems(List<OrderItems> orderItems) => _orderItems = orderItems;
Content.fromJson(Map<String, dynamic> json) {
_compCode = json['comp_code'];
_compName = json['comp_name'];
_orderNo = json['order_no'];
if (json['order_items'] != null) {
_orderItems = new List<OrderItems>();
json['order_items'].forEach((v) {
_orderItems.add(new OrderItems.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['comp_code'] = this._compCode;
data['comp_name'] = this._compName;
data['order_no'] = this._orderNo;
if (this._orderItems != null) {
data['order_items'] = this._orderItems.map((v) => v.toJson()).toList();
}
return data;
}
}
class OrderItems {
String _compCode;
String _compName;
String _orderNo;
OrderItems({String compCode, String compName, String orderNo}) {
this._compCode = compCode;
this._compName = compName;
this._orderNo = orderNo;
}
String get compCode => _compCode;
set compCode(String compCode) => _compCode = compCode;
String get compName => _compName;
set compName(String compName) => _compName = compName;
String get orderNo => _orderNo;
set orderNo(String orderNo) => _orderNo = orderNo;
OrderItems.fromJson(Map<String, dynamic> json) {
_compCode = json['comp_code'];
_compName = json['comp_name'];
_orderNo = json['order_no'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['comp_code'] = this._compCode;
data['comp_name'] = this._compName;
data['order_no'] = this._orderNo;
return data;
}
}