需要在 flutter 中使用 nullsaftey 将当前位置添加到 firestore
need to add current location to firestore with nullsaftey in flutter
我使用以下软件包:
google_maps_flutter:
cloud_firestore: ^2.4.0
location: ^4.2.0
我有这个代码:
storUserLocation()async{
Location location = new Location();
location.onLocationChanged.listen((LocationData currentLocation) {
FirebaseFirestore.instance.collection('parking_location').add({
'location' : GeoPoint(currentLocation.latitude, currentLocation.longitude)
});
它 returns this error.
LocationData.longitude
和 LocationData.latitude
returns double?
这意味着 return 值可以为空。
您可以通过添加检查来消除错误,以确保在使用它们之前值不为空。结帐如下:
location.onLocationChanged.listen((LocationData currentLocation) {
if (currentLocation.latitude != null && currentLocation.longitude != null) {
FirebaseFirestore.instance.collection('parking_location').add({
'location' : GeoPoint(currentLocation.latitude, currentLocation.longitude)
});
}
}
阅读有关空安全的更多信息here。
我使用以下软件包:
google_maps_flutter:
cloud_firestore: ^2.4.0
location: ^4.2.0
我有这个代码:
storUserLocation()async{
Location location = new Location();
location.onLocationChanged.listen((LocationData currentLocation) {
FirebaseFirestore.instance.collection('parking_location').add({
'location' : GeoPoint(currentLocation.latitude, currentLocation.longitude)
});
它 returns this error.
LocationData.longitude
和 LocationData.latitude
returns double?
这意味着 return 值可以为空。
您可以通过添加检查来消除错误,以确保在使用它们之前值不为空。结帐如下:
location.onLocationChanged.listen((LocationData currentLocation) {
if (currentLocation.latitude != null && currentLocation.longitude != null) {
FirebaseFirestore.instance.collection('parking_location').add({
'location' : GeoPoint(currentLocation.latitude, currentLocation.longitude)
});
}
}
阅读有关空安全的更多信息here。