dart - 如何在 http 调用中修复奇怪的飞镖错误
问题描述
我在运行时收到以下错误,我正在执行 http 调用并返回 json。
“_TypeError(类型'List'不是类型'() => void'的子类型)”
这是我的代码
class _ForumPostsState extends State<ForumPosts> {
List data;
String categoryID = 'D64D0737-746D-4562-8C10-6445F4069A92';
Future<String> getPostsByCategory() async {
var response = await http.post(
Uri.encodeFull("http://api/ForumPostsByCategory"),
headers: {"Content-Type": "application/json",
'Accept': 'application/json',},
body: json.encode({'categoryID' : categoryID }));
this.setState(
data = json.decode(response.body)
);
print(data[1]["title"]);
return "Success!";
}
此行抛出错误
data = json.decode(response.body)
在调试时,我注意到 JSON 在那里,它只是 data = json.decode 调用上的错误。
解决方案
改变这个:
this.setState(
data = json.decode(response.body)
);
对此:
this.setState(() {
data = json.decode(response.body)
}
);
更多信息:https ://docs.flutter.io/flutter/widgets/State/setState.html
推荐阅读
- arrays - 不使用 IML 重塑 SAS 表
- ios - UIContextMenu 和 UITableView 问题
- angular - 数据表行在更改角度排序顺序时被隐藏
- django - Gitlab runner 删除服务器上的一些文件/文件夹
- java - 在 Spring、Spock 和 Groovy 的测试中注入 @Value 失败
- python - 如何为 Postgresql 自动授权客户端?
- python - 您如何计算时间序列数据中的累积中位数?
- javascript - 如果列表包含任何元素,如何签入javascript?
- postgresql - 客户数据访问
- java - 为什么在 Windows 10 上为一个 Java 程序运行 2 个 Java 平台二进制文件?