[英]In Flutter how can I return an object after completion of Future in nested async functions?
I have a function which calls various other functions that return a Future and after the completion of one Future another method that return a future is called and in the end i need to return a value as a variable but the problem is that function returns NULL instead of the value. 我有一个函数,该函数调用其他各种返回Future的函数,在完成一个Future之后,又调用了另一个返回Future的方法,最后我需要返回一个值作为变量,但问题是该函数返回NULL而不是价值。
_getLocationPermission() waits and gets the required permission, i have to wait until I get the permission after getting permission I have to call _getCurrentLocation() which will return a Future < LocationData > and i have to pass this object's data to getWeatherDetails() and it will eventually return a Future< String > and i don't know how can i return this string in the return statement. _getLocationPermission()等待并获得所需的许可权,我必须等到获得许可权后才获得许可权,我必须调用_getCurrentLocation() ,这将返回Future <LocationData>,并且我必须将此对象的数据传递给getWeatherDetails()并它最终将返回Future <String>,我不知道如何在return语句中返回此字符串。
Future<String> getData() async {
String longitude, latitude, weatherDetails;
_getLocationPermission().then((permissionStatus) {
_getCurrentLocation().then((location) async {
longitude = location.longitude.toString();
latitude = location.latitude.toString();
weatherDetails = await getWeatherDetails(longitude, latitude);
print(longitude);
});
});
return weatherDetails;
} }
Thanks! 谢谢!
You seem to be returning a resolve aync response from the getWeatherDetails function not a Future as your function return type shows. 您似乎从getWeatherDetails函数返回了一个解析的aync响应,而不是函数返回类型所显示的Future。
Future<String> getData() async {
var weather;
_getLocationPermission().then((_){
var location = await _getCurrentLocation();
weather = getWeatherDetails(location.longitude.toString(), location.latitude.toString());
})
.catchError((error){
// Handle if location permission fails
});
return weather;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.