![](/img/trans.png)
[英]Flutter Firestore How to load array from firestore to a list
[英]How to get geopoints from Firebase Firestore as List<LatLng> for a Polyline in Flutter?
我的 firestore 数据库中有一组文档。 这是文件之一:
在我的应用程序中,我想根据文档中的点在 Google Map 上显示折线。 折线需要点作为纬度和经度列表,如下所示:
points: <LatLng>[
LatLng(0, 0),
LatLng(1, 1),
],
但是,如果我从 firestore 获得积分,它会将积分作为List<dynamic>
而不是List<LatLng>
接收。
这是错误:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<LatLng>'
我的问题是:如何从我的 Firestore 数据库中获取点作为List<LatLng>
或如何在数据库中编写 LatLngs,以便无论数据库中的列表有多少点,我都可以构建到 Map 的折线。
提前致谢!
使用map方法将List<dynamic>
转换为List<LatLng>
。
假设points数组仅由 GeoPoint 对象组成:
final db = FirebaseFirestore.instance;
await db.collection("your-collection-name").get().then((event) {
for (var doc in event.docs) {
final docData = doc.data();
List<dynamic> points = docData['points'];
final latlngPoints = points.map((e) => LatLng(e.latitude, e.longitude));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.