函数返回未来<dynamic>



所以我正在学习扑动,我有一个返回UserLocation对象的函数-

getUserLocation() async {
bool _serviceEnabled;
loc.PermissionStatus _permissionGranted;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == loc.PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != loc.PermissionStatus.granted) {
return;
}
}
try {
_currentPosition = await location.getLocation();
} catch (e) {
print(e);
}
List<geo.Placemark> placemarks = await geo.placemarkFromCoordinates(
_currentPosition.latitude ?? 0, _currentPosition.longitude ?? 0);
var countryNameList = placemarks[0].country?.split(' ');
if (countryNameList!.isNotEmpty && countryNameList.length >= 2) {
for (var eachLetter in countryNameList) {
abbr += eachLetter[0];
}
} else {
abbr = countryNameList.toString().substring(0, 2).toUpperCase();
}
return UserLocation(
city: placemarks[0].locality ?? 'Chennai',
country: abbr,
latitude: _currentPosition.latitude,
longitude: _currentPosition.longitude);
}

现在,当我调用这个函数,它说它返回Future<动态)..Future,因为它是async函数而dynamic,因为它不会返回任何东西。ServiceEnabled或location。hasPermission失败。>

无论如何,关键是我想访问这个方法返回的UserLocation对象,每当我从其他地方调用这个函数,但它总是说这个函数返回未来。我怎么能做到??任何想法?

这应该可以解决问题,将getUserLocation()函数的返回类型设置为Future<UserLocation?>,并在没有任何可返回的地方返回null。

相关内容

最新更新