I've got a collection in my firebase firestore database called 'bakeries' and inside the collection has a series of documents, each with a geopoint field, where I typed in their latitudes and longitudes. To access them and see the nearest bakeries to the users, I created a ListView.builder. But, I'm trying to sort the bakeryList by its geopoint in relation to the user's current location and it's not working. I've tried this:
bakeryList.sort((a, b){
return a['geopoint'].compareTo(myLocation);
});
But it's not returning the nearest bakeries. Any ideas would be hugely appreciated!
final userLocation = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
StreamSubscription<QuerySnapshot> subscription;
List <DocumentSnapshot> bakeryList;
final Query collectionReference = Firestore.instance.collection(('bakeries'));
@override
void initState() {
super.initState();
final myLocation = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
subscription = collectionReference.snapshots().listen((data) {
setState(() {
bakeryList = data.documents;
});
});
}
@override
Widget build(BuildContext context) {
return bakeryList != null ?
ListView.builder(
itemCount: bakeryList.length,
itemBuilder: (context, index){
String imgPath = bakeryList[index].data['image'];
String bakeryTextPath = bakeryList[index].data['name'];
String locationNamePath = bakeryList[index].data['location name'];
GeoPoint geoPointPath = bakeryList[index].data['geopoint'];
final geolocation = Text('${geoPointPath.latitude}, ${geoPointPath.longitude}');
return BakeryCard(
etaText: '${geoPointPath.latitude}, ${geoPointPath.longitude}',
locationText: locationNamePath,
merchantText: bakeryTextPath,
assetImage: Image.network(imgPath),
function: (){});
})
: Center(child: CircularProgressIndicator(),
);
}