gpt4 book ai didi

flutter - 引发了一个异常:NoSuchMethodError:在null上调用了getter 'the0'

转载 作者:行者123 更新时间:2023-12-03 03:38:22 25 4
gpt4 key购买 nike

我正在尝试使用Rest Service从API获取数据。
但是我得到一个错误,说getter被调用为null,在进行了一些研究之后,我发现了一些东西,例如UI,要显示的数据比getData函数先执行,它使系统将变量读取为null并且发生错误。任何人都可以帮我解决这个问题。

这是我的一些代码,

class PickUp extends StatefulWidget {
var created_name, wmsorders_id, id;
PickUp(
{Key key,
@required this.created_name,
@required this.wmsorders_id,
@required this.id})
: super(key: key);

@override
_PickUpState createState() => _PickUpState();
}

class _PickUpState extends State<PickUp> {
DetailModel detailModel;
String sender = "";

Future<String> getDetail() async {
print("id : " + widget.id);
var data = "id=" + widget.id + "";
var response_detail = await RestService()
.restRequestServiceGet(SystemParam.URL_DETAIL_UPCOMING, data);
print("response_detail : " + response_detail.body.toString());

setState(() {
detailModel = DetailModel.fromJson(jsonDecode(response_detail.body));

});

return "Success!";
}

@override
void initState() {
getDetail();
}

Widget build(BuildContext context) {

// NULL CHECKING
if (detailModel != null) {
print("sender =" +detailModel.the0.picName);
} else {
print("sender = null");
}

// I want to get picName from detail Model and using it in UI, but I got Error here

sender = detailModel.the0.picName';
print("sender = " +'$sender');

}


这是detailModel
// To parse this JSON data, do
//
// final detailModel = detailModelFromJson(jsonString);

import 'dart:convert';

DetailModel detailModelFromJson(String str) => DetailModel.fromJson(json.decode(str));

String detailModelToJson(DetailModel data) => json.encode(data.toJson());

class DetailModel {
The0 the0;
The0 the1;
Records records;

DetailModel({
this.the0,
this.the1,
this.records,
});

factory DetailModel.fromJson(Map<String, dynamic> json) => DetailModel(
the0: The0.fromJson(json["0"]),
the1: The0.fromJson(json["1"]),
records: Records.fromJson(json["records"]),
);

Map<String, dynamic> toJson() => {
"0": the0.toJson(),
"1": the1.toJson(),
"records": records.toJson(),
};
}

class Records {
int status;
String message;

Records({
this.status,
this.message,
});

factory Records.fromJson(Map<String, dynamic> json) => Records(
status: json["status"],
message: json["message"],
);

Map<String, dynamic> toJson() => {
"status": status,
"message": message,
};
}

class The0 {
String id;
String sku;
int sak;
String qty;
String shipstatId;
String picName;
String picTelp;
String orderMultipleId;
String orderdetId;
String coordinatorId;

The0({
this.id,
this.sku,
this.sak,
this.qty,
this.shipstatId,
this.picName,
this.picTelp,
this.orderMultipleId,
this.orderdetId,
this.coordinatorId,
});

factory The0.fromJson(Map<String, dynamic> json) => The0(
id: json["id"],
sku: json["sku"],
sak: json["sak"],
qty: json["qty"],
shipstatId: json["shipstat_id"],
picName: json["pic_name"],
picTelp: json["pic_telp"],
orderMultipleId: json["order_multiple_id"],
orderdetId: json["orderdet_id"],
coordinatorId: json["coordinator_id"],
);

Map<String, dynamic> toJson() => {
"id": id,
"sku": sku,
"sak": sak,
"qty": qty,
"shipstat_id": shipstatId,
"pic_name": picName,
"pic_telp": picTelp,
"order_multiple_id": orderMultipleId,
"orderdet_id": orderdetId,
"coordinator_id": coordinatorId,
};
}


这是错误,

enter image description here

最佳答案

您需要在构建方法中使用FutureBuilder,然后等待响应。

删除setstate并按如下所示修改代码。

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Test',
style: TextStyle(
color: Colors.white,
),
),
),
body: FutureBuilder<DetailModel>(
future: getDetail(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print("Here you can get data "+snapshot.data.toString());
} else {
print("Waiting mode");
return Container(
color: Colors.blue,
);
}
},
),
);
}
}

关于flutter - 引发了一个异常:NoSuchMethodError:在null上调用了getter 'the0',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com