GeoFlutterFire Flutter plugin for Firestore not return distance field
Asked Answered
M

2

5

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')}");
    }
Misadventure answered 14/4, 2020 at 19:52 Comment(0)
S
5

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

Syllepsis answered 18/4, 2020 at 10:1 Comment(2)
hey, how would I pass the distance variable to the functionFreytag
Where do you want to pass the distance variable? it returns the distance of type double when you call distance method on a GeoFirePoint passing the longitude and latitude in it.Syllepsis
M
1

Returning distance with geoflutterfire

Get user location

Geolocator userLocation = Geolocator()

Convert user location to GeoPoint

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

From your DocumentSnapshot List data;

Convert the List data to lat long

var latitude = data[index].data()['position']['geopoint'].latitude;

var longitude = data[index].data()['position']['geopoint'].longitude

Use the lat long to get distance with

point.distance(latitude, longitude)

var distance = point.distance(lat: latitude, lng: longitude);
Myrnamyrobalan answered 23/8, 2020 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.