如何根据距离对用户进行排序,即安排他们有最近的用户?,我可以将用户带到一定的半径内,而不需要排序

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

我可以将一定半径内的用户带到当前用户(即10公里范围内的用户)。现在我想根据它们的距离对它们进行排序,将最近的列在第一位。 为了解决这个问题,我尝试了以下代码,但出现错误:

错误: 收到此错误:"Unhandled Exception: PlatformException(error, Invalid Query. 'in' filters cannot contain 'null' in the value array., null)" –

详细代码:

临时模型:(用于存储用户 ID 和地理位置的集合)

 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});
  }

变量:

  List<certainrangeusers> info= [];

代码:

       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;
             });
          });
      }

屏幕截图:[在粉红色框区域中,完成了整个计算并分配了 id,并且只有那些来自 firebase 用户集合的文档 ]

这可以通过在 for 循环本身中添加 "id" 轻松解决, 如下图所示:

       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
        }