firebase - “String”类型不是“double”类型的子类型
问题描述
我想构建一个在谷歌地图上绘制标记的应用程序。这仍然很容易,但我希望 firebase 为标记提供纬度和经度值。我用下面的代码尝试了它,一切正常(我可以打印两个值),除了标记被绘制。
这是我试过的代码:
var boatLatitude;
var boatLongitude;
void getBoatLocation() {
FirebaseDatabase.instance
.reference()
.child('latitudeBoot')
.onValue
.listen((event) {
print('-----------------------' + event.snapshot.value);
setState(() {
boatLatitude = event.snapshot.value;
});
});
FirebaseDatabase.instance
.reference()
.child('longitudeBoot')
.onValue
.listen((event) {
print(event.snapshot.value + '----------------------');
setState(() {
boatLongitude = event.snapshot.value;
});
Marker c = Marker(
markerId: MarkerId('3'),
icon: customIconBoat,
position: LatLng(boatLatitude, boatLongitude));
setState(() {
markers.add(c);
});
});
}
我在应用程序启动时调用上面的函数。
问题是我收到此消息,但我不知道如何解决问题:
E/flutter (22304): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'double'
解决方案
更改此行
position: LatLng(boatLatitude, boatLongitude));
对此:
position: LatLng(double.parse('$boatLatitude'), double.parse('$boatLongitude')));