【发布时间】:2021-12-31 20:39:20
【问题描述】:
getInitialTripData 使用 await 调用 getCurrentLocationData,但它从不打印“来这里 1”,并且在 getCurrentLocationData 上,该函数继续运行直到打印数据,然后卡在返回语句上,我不知道问题是什么
setInitialTripData() async {
print("coming here");
MapLocation startingPoint=await Get.find<LocationService>().getCurrentLocationData();
print("coming here 1");
if (startingPoint != null) {
tripData.startingPoint = startingPoint;
startingPointTextController.text = startingPoint.name;
update();
}
}
Future<MapLocation> getCurrentLocationData()async{
try{
if(!locationAllowed.value){
return null;
}
LocationData position=await _location.getLocation();
List<geo.Placemark> placemark =
await geo.placemarkFromCoordinates(position.latitude, position.longitude);
if(placemark.length==0 || placemark==null){
return null;
}
MapLocation mapLocation=MapLocation(name: "${placemark[0].name.isNotEmpty? placemark[0].name+", ":""}${placemark[0].subAdministrativeArea.isNotEmpty? placemark[0].subAdministrativeArea+", ":""}${placemark[0].isoCountryCode.isNotEmpty? placemark[0].isoCountryCode:""}",latitude: position.latitude,longitude: position.longitude);
print(mapLocation.getDataMap());
return mapLocation;
}
catch(e){
return null;
}
}
【问题讨论】:
-
开启空值安全。一点点的痛苦会带来很大的收获。你也在使用 lints 包吗?
标签: flutter dart async-await