问题
I'm fetching coordinates from Firestore to fill my map with stores locations. The Google Map Marker needs a LatLng object (that takes two double
as parameters) with the latitude and longitude of a point. But, for each increment, I get the following exception: type 'String' is not a subtype of type 'double'
(verbose down below).
However, the points do appear on the map, which should mean that the values ARE of type double...
I used the following code to identify the latitude and longitude type:
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('');
It says "double!" each time.
Here is the code:
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 */
}));
This is the full error:
[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<…>
EDIT:
This is the full verbose output for 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 _<…>
回答1:
Use the parseDouble
function that returns null if the Object
provided can not be parsed to a double
. You can then check if the values of the longitude and latitude are null before creating the marker.
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 */
}
}));
parse Function
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;
}
}
来源:https://stackoverflow.com/questions/56253227/unhandled-exception-type-string-is-not-a-subtype-of-type-double-even-if-a-d