未处理的异常:即使给定了double,类型'String'也不是类型'double'的子类型 [英] Unhandled Exception: type 'String' is not a subtype of type 'double' even if a double is given
问题描述
我正在从Firestore提取坐标,以用商店位置填充我的地图.Google Map Marker需要一个LatLng对象(该对象需要两个 double
作为参数),并具有点的经度和纬度.但是,对于每个增量,我都会遇到以下异常:类型'String'不是类型'double'的子类型
(下面详细说明).
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).
但是,这些点确实出现在地图上,这意味着值的类型为double ...
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('');
它说加倍!"每次.
这是代码:
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'])
的完整详细输出:
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 _<…>
推荐答案
使用 parseDouble
函数,如果提供的 Object
无法解析为双
.然后,您可以在创建标记之前检查经度和纬度的值是否为空.
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 */
}
}));
解析功能
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;
}
}
这篇关于未处理的异常:即使给定了double,类型'String'也不是类型'double'的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!