简体   繁体   中英

How to get geopoints from Firebase Firestore as List<LatLng> for a Polyline in Flutter?

I have a collection of documents in my firestore database. This is one of the documents: 在此处输入图像描述

In my app I want to show Polylines on the Google Map based on the points in the document. The Polyline requires points as a List of Latitudes und Longitudes like this:

points: <LatLng>[
  LatLng(0, 0),
  LatLng(1, 1),
],

However if I get the points from firestore it receives the points as a List<dynamic> and not a List<LatLng> .

This is the error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<LatLng>'

My question is: How can I get the points from my Firestore database as a List<LatLng> or how can I write LatLngs in the database so I can build Polylines to the Map no matter how much points the list in the database has.

Thanks in advance!

Convert List<dynamic> into List<LatLng> using map method.

Assuming points array only consists of GeoPoint objects:

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));
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM