【发布时间】:2021-11-01 12:40:35
【问题描述】:
我有这个:
class HonPage extends StatefulWidget {
final Status? status;
HonPage({Key? key, this.status}) : super(key: key);
@override
_HonPageState createState() {
return _HonPageState();
}
}
class _HonPageState extends State<HonPage> {
@override
void initState() {
super.initState();
}
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return new ListTile(
title: new Text("${widget.status.title ?? ""}"),
);
}
);
}
}
我的结果:
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
无论我做什么,我似乎都无法从JSON 获取项目。
JSON 和 API URL 适用于所有其他屏幕,因此不会出错。我在这里做错了什么?只是我新建了一个tab,想显示信息。
编辑: 在 Status.dart 中,我有这个:
Status(Map<String, dynamic> json) : super(json) {
status = json["status"];
title = json["title"];
....
}
JSON:
[{"title": "NAME ME", "status": "publish"}]
【问题讨论】:
-
看来我们在这里遗漏了很多上下文。 JSON?获取项目?这些都不在你的代码中......
-
您向我们展示的内容与您遇到的问题无关。 NULL 值来自 ${widget.status.title},所以向我们展示你是如何获取数据的,status.title 来自哪里
-
查看编辑后的帖子。
-
您还没有发布指示“widget.status.title”应该不为空的行。
-
现在可以了吗?
标签: json flutter api rest dart