flutter - type '(dynamic) => Null' 不是 '(String, dynamic) => void' of 'f' 的子类型
问题描述
我正在尝试填充从 Web API 检索到的数据:
void asyncInitState() async {
BulletinController bulletinController = BulletinController() ;
var value = await bulletinController.AfficherEtablissement();
value.forEach((entry) {
print(entry['id']);
});
}
@override
void initState() {
super.initState();
asyncInitState();
super.initState();
}
这是“AfficherEtablissement()”方法:
Future<dynamic> AfficherEtablissement() async {
final response =
await http.get('https://jsonplaceholder.typicode.com/albums/1');
if (response.statusCode == 200) {
var parsedJson = json.decode(response.body);
//decode
print(parsedJson);
Map<String, dynamic> data = json.decode(response.body);
return data ;
} else {
throw Exception('Failed to load');
}
}
我收到此错误:
type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'f'
在他的行中:
print(entry['id']);
解决方案
AfficherEtablissement函数返回一个Map对象,因此对于响应迭代,您应该使用:
value.forEach((key, value) {
// do something
});
请注意,您传递给 forEach 的函数有两个参数(键、值)。
推荐阅读
- mysql - 使用来自该表本身的选定值的总和更新表
- php - 将两个数组元素合并为一个新元素
- android - GoogleApiclient 中的 enableAutoManage 编译失败
- android - 设置顶部填充影响视图的底部边距
- sql - 如何聚合具有计数和虚拟值的联合表?
- c# - 使用 System.Diagnostics.Process 运行 vb 脚本,但仅部分成功地将输入文本输入到进程的标准输入
- ios - 我应该将我的 API 密钥存储在 IOS 应用程序的什么位置
- c# - 如何使用 json.net 反序列化嵌套类?
- image - 如何在颤动中向图像添加文本
- javascript - 单击浏览器上的后退按钮时如何加载确切的页面?