Search code examples
firebasefluttergoogle-cloud-firestoregeolocationgeofire

How to sort users according to distances,i.e arrange them has nearest users?,i am able to bring users within certain radius, without sorting


I am able to bring users in who are in certain radius to the current user(i.e, users within the range of 10km). Now I want to sort them according to their distance, list the nearest ones at the first. I have tried the following code, in order to solve this, but got an error:

Error: Got this error: "Unhandled Exception: PlatformException(error, Invalid Query. 'in' filters cannot contain 'null' in the value array., null)" –

Detailed Code:

Temp model:(for the collection which stores users ids and geoposition)

 class certainrangeusers {
 final String id;//users ids
 final double away;//to store distance
 final GeoPoint userpoint;//other users geopoints
  certainrangeusers({this.id, this.away, this.userpoint});
  }

Variables:

  List<certainrangeusers> info= [];

code:

       getUsers() async
         {
          double radius = 0.3;
            String field = "GeoPosition";
             print(currentUser.point.longitude);
                GeoFirePoint center = geo.point(latitude: currentUser.point.latitude,
     longitude: currentUser.point.longitude); // Here to calculate the geopositon, geohash and geopoint of the current user

   var collectionRef = Firestore.instance.collection('user_locations').document(currentUser.pincode)
     .collection('Users_comp  lete_address_and_geopoint');

      this.stream =  geo.collection(collectionRef: collectionRef)
          .within(center: center, radius: radius, field: field, strictMode: false);

        Future<List<certainrangeusers>> users1 =  stream.first.then((documents) => documents.map((doc) => 
      certainrangeusers(
     id: doc['userPhone'],
    userpoint : doc['GeoPosition']['geopoint'], // here other users geopoints who are in 300mteres range are stored
   )
    ).toList());

      users1.then((val) async{
       for(var value in val){ // this loop is to calculate the distance of the current user to the other users 
        info.add(certainrangeusers(
        away: await Geolocator().distanceBetween(currentUser.point.latitude,currentUser.point.longitude, value.userpoint.latitude, value.userpoint.longitude),
          //away: center.distance(lat: value.userpoint.latitude,lng: value.userpoint.longitude),
                     ));
              info.sort((a,b) => a.away.compareTo(b.away));//here we r sorting them
            }
          List ids = [];
          for(var value in info){
         ids.add(value.id);// adding them to empty list in order to bring them according to their ids sorted
             }
        QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments(); // here we are passing the above list of ids
       List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
        setState(() {
        this.users = users;
             });
          });
      }

Screenshot:[In the pink box area, whole calculation is done and ids assigned and only those documents brought from firebase users collection]

Click here for the screenshot of the above, where specific place, I was struck


Solution

  • This can easily solved by adding "id" in the for loop itself, like shown below:

           for(var value in val){ // this loop is to calculate the distance of the current user to the other users 
                  info.add(certainrangeusers(
                       away: await Geolocator().distanceBetween(currentUser.point.latitude,currentUser.point.longitude,value.userpoint.latitude, value.userpoint.longitude),
                       id: value.id,
                     ));
              info.sort((a,b) => a.away.compareTo(b.away));//here we r sorting them
            }