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.
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')}");
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With