我正在从Firestore中获取坐标,以使用商店位置填充地图。 Google Map Marker需要一个LatLng对象(以两个double
作为参数),该对象具有一个点的纬度和经度。但是,对于每个增量,我都会收到以下异常:type 'String' is not a subtype of type 'double'
(在下面进行详细说明)。
但是,这些点确实出现在地图上,这意味着值的类型为double ...
我使用以下代码来识别纬度和经度类型:
if (store['latitude'] is double) {
print('double!');
} else if (store['latitude'] is String) {
print('string!');
}
print('');
if (store['longitude'] is double) {
print('double!');
} else if (store['longitude'] is String) {
print('string!');
}
print('');
它说“加倍!”每次。
代码如下:
Firestore.instance
.collection('points-of-sale')
.snapshots()
.listen((data) => data.documents.forEach((store) {
LatLng pos = LatLng(
store['latitude'], // Here
store['longitude'] // and here
);
/* Adding a marker to the map */
}));
这是完整的错误:
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'double'
#0 _StoreLocatorState._addMarker (package:leonor_greyl_app_2019_41820/pages/StoreLocatorWidget.dart:21:30)
#1 _StoreLocatorState.build.<anonymous closure>.<anonymous closure> (package:leonor_greyl_app_2019_41820/pages/StoreLocatorWidget.dart:48:15)
#2 List.forEach (dart:core-patch/growable_array.dart:278:8)
#3 _StoreLocatorState.build.<anonymous closure> (package:leonor_greyl_app_2019_41820/pages/StoreLocatorWidget.dart:47:42)
#4 _rootRunUnary (dart:async/zone.dart:1132:38)
#5 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#6 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#7 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#8 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
#9 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
#10 _PendingEvents.schedule.<anonymous closure> (dart:a<…>
编辑:
这是parseDouble(store['latitude'])
的完整详细输出:
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: FormatException: Invalid double
x
#0 double.parse (dart:core-patch/double_patch.dart:110:28)
#1 _StoreLocatorState._addMarker (package:leonor_greyl_app_2019_41820/pages/StoreLocatorWidget.dart:22:66)
#2 _StoreLocatorState.build.<anonymous closure>.<anonymous closure> (package:leonor_greyl_app_2019_41820/pages/StoreLocatorWidget.dart:51:15)
#3 List.forEach (dart:core-patch/growable_array.dart:278:8)
#4 _StoreLocatorState.build.<anonymous closure> (package:leonor_greyl_app_2019_41820/pages/StoreLocatorWidget.dart:50:42)
#5 _rootRunUnary (dart:async/zone.dart:1132:38)
#6 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#9 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
#10 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
#11 _<…>
答案 0 :(得分:0)
使用parseDouble
函数,如果提供的Object
无法解析为double
,则该函数返回null。然后,您可以在创建标记之前检查经度和纬度的值是否为空。
Firestore.instance
.collection('points-of-sale')
.snapshots()
.listen((data) => data.documents.forEach((store) {
double latitude = parseDouble(store['latitude']);
double longitude = parseDouble(store['longitude']);
// Check if parser returns null for non-parsable string
if (latitude != null && longitude != null) {
LatLng pos = LatLng(latitude, longitude);
/* Adding a marker to the map */
}
}));
解析功能
double parseDouble(dynamic value) {
try {
if (value is String) {
return double.parse(value);
} else if (value is double) {
return value;
} else {
return 0.0;
}
} catch (e) {
// return null if double.parse fails
return null;
}
}