Flutter 如何从 Future Function 访问地图
Flutter how to access the map from Future Function
dart 新手,我想访问 Map
对象中的值 userId
,但我一直收到错误(见评论):
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
) 时,您将方法作为对象传递,而不是调用方法并检索其 return 值。
要解决此问题,只需通过提供括号来调用该方法:
var getit = getData();
dart 新手,我想访问 Map
对象中的值 userId
,但我一直收到错误(见评论):
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
) 时,您将方法作为对象传递,而不是调用方法并检索其 return 值。
要解决此问题,只需通过提供括号来调用该方法:
var getit = getData();