dictionary - Flutter 如何从 Future Function 访问地图
问题描述
飞镖新手,我想访问对象userId
内部的值Map
,但我不断收到错误(见评论):
The method 'then' isn't defined for the type 'Function'.
Try correcting the name to the name of an existing method, or defining a method named 'then'.
Future<Map> getData() async {
Response load = await get('https://jsonplaceholder.typicode.com/todos/1');
print(load.statusCode);
print(load.body);
Map map = jsonDecode(load.body);
return map;
}
@override
void initState() {
var getit = getData;
print('placeholder');
getit.then((e){e['userId'];}); // I get an Error here
super.initState();
}
解决方案
问题源于线路var getit = getData;
当您提供不带括号 ( getData
) 的方法名称时,您将方法作为对象传递,而不是调用该方法并检索其返回值。
要解决此问题,只需通过提供括号来调用该方法:
var getit = getData();
推荐阅读
- node.js - 将嵌套的 JSON 数据输出到节点 JS 变量
- ionic-framework - Ionic 4 背景闪烁和点击延迟
- attachment - 有没有办法一次保存收到的电子邮件+附件?
- sql - SQL 报表过滤,结果在第一项之后停止
- jquery - free-jqgrid - getRowData 为不在当前页面内的行返回空
- php - 从 PHP 运行屏幕并使用东西
- javascript - JavaScript - 当 if 语句为真时,代码不执行?
- java - 处理 Firebase 中的用户存在
- java - 图像未显示在 JFrame 中
- visual-studio - 一些 Visual Studio 文件的 lint 会根据它们的创建方式而有所不同,即使它们最终完全相同。为什么?