Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeoFlutterFire Flutter plugin for Firestore not return distance field

I'm using GeoFlutterFire, a plugin for Flutter which sifts a collection for records that are near a specified GeoFirePoint.
A distance key is supposed to be returned with the Firestore <DocumentSnapshot>s but none is, only the default keys. When I seek it as below it returns null. The key doesn't exist.

  1. I've tried changing the emulators GPS location, and then the usersAddressGeoFirePoint, no luck.
  2. The instructions aren't clear on whether GeoFire creates this field, or needs it to pre-exist as a Firestore field. I've tried creating the Firestore field manually with a dummy value and then the value I get from the download is not the distance between the 2 geopoint points but the dummy value it was set on Firestore.

This is great frustration, I need the distance so I can show it on Cards in the app, my code is very close to the example code for the plugin, so I can't see why it doesn't work.

GeoFirePoint usersAddressGeoFirePoint = 
        geo.point(latitude: 56.088944, longitude: -4.151800);
    
localJobsStream = geo.collection(collectionRef: myFirestore.collection('JobsMeta'))
            .within(center: usersAddressGeoFirePoint, radius: 1000, field: 'position', strictMode: true);
    
localJobsStream.listen((List<DocumentSnapshot> documentList) {
    ${documentList[0].data.keys.contains('distance')}");
    }
like image 306
Sam Avatar asked Apr 14 '20 19:04

Sam


1 Answers

I checked the Geoflutterfire's within(center: center, radius: radius, field: field, strictMode: strictMode) method and it is returning Stream<List<DocumentSnapshot>> as expected but for distance calculation, it is using a wrapper class DistanceDocSnapshot which has a DocumentSnapshot and distance variable.

When I debugged the "within" method, I found that DistanceDocSnapshot has a variable distance of type double but they are not setting the distance value in DocumentSnapshot before returning.

So the package has the data but of no use until we get it. By the time they fix this issue or we get to know any other way to use it, you can use below code to get the distance as they are using the same in the package and mentioned in readme here GeoFlutterFire Package :

 GeoFirePoint center = geo.point(
     latitude: currentUserPos.latitude,
     longitude: currentUserPos.longitude,
 );

 double distance = center.distance(lat: geoPoint.latitude, lng: geoPoint.longitude);

Update on the same: As per change log on package page we would not be able to access data using doc.data['distance'] anymore with version 2.1.0

like image 96
Arpit Avatar answered Nov 15 '22 06:11

Arpit