【发布时间】:2023-03-24 08:17:01
【问题描述】:
我能够在 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 Model类如下
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;
}
}
我得到的错误在这里
以下 NoSuchMethodError 被抛出构建 PageViewClass(dirty, state: _PageViewClassState#98a84): getter 'content' 在 null 上调用。接收者:null 尝试调用:内容
我无法理解我的代码出了什么问题,以及如何解决它 请进一步指导
【问题讨论】:
-
@chunhunghan 嗨,我看到你解决了一个类似的问题,请帮我解决这个问题,我尝试实施你对这个问题的解决方案stackoverflow.com/questions/63555004/… 但没有成功,请进一步指导我跨度>
标签: arrays json flutter flutter-listview flutter-pageview