I'm trying to get the near by location using firebase query and it goes well. This the function that gets the nearby location.
Future<List<DocumentSnapshot>> nearbyLocations() async {
CollectionReference collectionRefer =
FirebaseFirestore.instance.collection('locations');
double radius = 10;
String field = 'position';
List<DocumentSnapshot> docList;
GeoFirePoint center = await getCurrentLocation();
// print(center);
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: collectionRefer)
.within(center: center, radius: radius, field: field, strictMode: true);
stream.listen((List<DocumentSnapshot> documentList) {
if (documentList.length > 0) {
print(documentList[0].data());
docList = documentList;
} else {
return {};
}
});
}
I know that the query will return only one data. So, I printed on the first value in the above function. The problem arises when the documentList is returned.
loadData() async {
documentList =
await GeoLocator().nearbyLocations();
}
When I call this above function, It prints null. But When I tried to print in the nearbyLocations() It print the data. But not when I Call loadData(). I'm going to use this returned data in listview.
nearbyLocations()is an async method. Either you need to make youloadData()async or add a callback tonearbyLocations()usingthen((value){ // print value here});loadData()and still afterthenIt prints null.