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/