dictionary - flutter 如何从 future 功能访问 map

标签 dictionary flutter dart future

dart的新手,我想访问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();

关于dictionary - flutter 如何从 future 功能访问 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62561802/

相关文章:

python - 嵌套字典的子集

Scala:添加到 Map 的奇怪现象

flutter - 在 Flutter 项目中为 iOS 导出存档时,Codemagic 或 FaSTLane 失败

android - Flutter-单击按钮时在GridView中添加容器(窗口小部件)?

dart - 我们如何在flutter中更改appbar背景颜色

android - 通过图标点击 flutter 打开设备上的联系人

dart - PercentCompleteEditor允许bwu_datagrid中的值> 100

java - 如何检查Map是否有重复的key?

python - 结合两个字典,仍然保留对 python 中原始对象的引用

selenium - 在 Flutter 应用程序中使用 dart 抓取动态网站