颤抖如何从未来功能访问地图

时间:2020-06-24 18:30:11

标签: 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();
  }

1 个答案:

答案 0 :(得分:1)

问题来自var getit = getData;

当您为方法名称提供不带括号(getData)时,您会将方法作为对象传递,而不是调用方法并获取其返回值。

要解决此问题,只需提供括号即可调用该方法:

var getit = getData();