【发布时间】:2019-12-19 13:16:51
【问题描述】:
我有一个模型类
LocalInfo.dart
class LocationInfo {
// LocationController
Location location = Location();
//Location Position Data
LocationData currentLocation;
double longitude;
double latitude;
LocationInfo() {
try {
location.getLocation().then((location) {
currentLocation = location;
longitude = currentLocation.longitude;
latitude = currentLocation.latitude;
});
} catch (e) {
currentLocation = null;
}
}
}
还有我的地图类 MapsService.dart
MapsService() {
loading = true;
locationInfo = LocationInfo(); // <== This takes some time
cameraPosition = CameraPosition(
zoom: 15,
target: LatLng(locationInfo.latitude, locationInfo.longitude)); // <-- Latitude Longitude is null
loading = false;
}
这里的问题是我创建了一个具有经度和纬度作为类成员的 LocationInfo 模型对象。但是由于 location.getLocation() 返回的是 Future,因此需要一些时间来获取这些值。
但是在我的 MapsService 类中,我创建了一个 LocationInfo 对象并将 cameraPosition 设置为给定的纬度和经度。但是我怎么能等待经度和纬度值呢? Long/Lat 为 null 并导致应用程序崩溃,因为我不等待。
我需要在我的 MapService 构造函数中等待 LocationInfo。它必须在我设置 Lat 和 Long 值之前完成
【问题讨论】: