Reading geopoint from firebase firestore using geoFlutterFire

Viewed 19

I have successfully pushed data into firebase firestore and trying to get it back , but facing some issues with that . i would appreciate any help , Thank you

  • This is my attempt
void getDrivers(){
     geoFlutterFire = Geoflutterfire();
     GeoFirePoint geoFirePoint = GeoFirePoint(latitude, longitude);

     var ref = _firebaseFirestore.collection('Drivers');
     var locations = geoFlutterFire.collection(collectionRef: ref)
         .within(center: geoFirePoint, radius: maxRadius, field: 'position');


     locations.listen((List<DocumentSnapshot> documentList) {
       for (var documentSnapshot in documentList) {
         Map<String,Object> documentShot = documentSnapshot.get('geopoint');
         dynamic lat = documentShot['Latitude'];
         dynamic lon = documentShot['Longitude'];
         markers.add(Marker(
             markerId: MarkerId(DateTime.now().millisecond.toString()),
             position: LatLng(lat,lon),
             icon: BitmapDescriptor.defaultMarker
         ));
         Fluttertoast.showToast(msg: 'markers Size ' +  markers.length.toString());
       }
     });
 }
  • This is an image of the firestore data enter image description here

  • Error Image

    enter code here

PS : error points to this line : documentSnapshot.get('geopoint');

1 Answers

The geopoint field exists inside the position field, so to access it you need:

documentSnapshot.get('position.geopoint');

Or:

documentSnapshot.get('position')["geopoint"];
Related