geoflutter fire running on Future Async instead of Streams?

163 Views Asked by At

is there a way to get only one Future call from the Geofire plugin instead of a Stream ? Streams are great to monitor locations, but if you just want to do a simple Geoquery, is there a way to cancel / pause the stream once all results are fetched ? or is there a way to query from a Future ?

On a map, I would like to query only when Map is on idle. From the geo query launching some serious async fetching. Having a Stream makes it very hard to maintain organized multi nested data fetched from various places in the back end, because it keeps updating itself.

1

There are 1 best solutions below

1
On

Needed to solve a similiar problem when the query is run only once. This way you can await for data and only update once the data is fetched. Did it as follows:

 getData(double lat, double long, double radius) async {
GeoFirePoint center = geo.point(latitude: lat, longitude: long);
var collectionReference = _firestore.collection('mosques');
double radius = 50;
//Your geohash field goes here: 
String field = 'location.geohash';
Stream<List<DocumentSnapshot>> stream = geo
    .collection(collectionRef: collectionReference)
    .within(center: center, radius: radius, field: field);
List<DocumentSnapshot> documents = await stream.first;
if (documents.isNotEmpty) {
  //Whatever needs to be done
  }).toList();
  
} else {
  return [];
}

}