如何对模型中表示特定集合的firebase文档的特定项进行排序?(注:点击下面的答案)

如何对模型中表示特定集合的firebase文档的特定项进行排序?(注:点击下面的答案),firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我想对模型中表示特定集合的firebase文档的项目进行排序。此外,此模型包含集合“用户”的用户(,将根据距离进行排序),我将仅从“用户”中获取这些文档收藏我已经计算了当前用户与其他用户之间的距离,我希望为该用户带来最近的用户我尝试了以下代码: class certainrangeusers { final String id; final double away; final GeoPoint userpoint; certainrangeu

我想对模型中表示特定集合的firebase文档的项目进行排序。此外,此模型包含集合“用户”的用户(,将根据距离进行排序),我将仅从“用户”中获取这些文档收藏我已经计算了当前用户与其他用户之间的距离,我希望为该用户带来最近的用户我尝试了以下代码:

     class certainrangeusers {
     final String id;
     final double away;
     final GeoPoint userpoint;
      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);

       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'],
       )
        ).toList());

          users1.then((val) async{
           for(var value in val){
            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));
                }
              List ids = [];
              for(var value in info){
             ids.add(value.id);
                 }
            QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
           List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
            setState(() {
            this.users = users;
                 });
              });
          }
屏幕截图:[在粉红框区域,完成并分配整个计算,并从firebase获取文档] []

出现错误:

     class certainrangeusers {
     final String id;
     final double away;
     final GeoPoint userpoint;
      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);

       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'],
       )
        ).toList());

          users1.then((val) async{
           for(var value in val){
            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));
                }
              List ids = [];
              for(var value in info){
             ids.add(value.id);
                 }
            QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
           List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
            setState(() {
            this.users = users;
                 });
              });
          }
收到此错误:“未处理的异常:PlatformException(错误,无效查询。'in'筛选器不能在值数组中包含'null',null)”–

详细代码:

     class certainrangeusers {
     final String id;
     final double away;
     final GeoPoint userpoint;
      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);

       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'],
       )
        ).toList());

          users1.then((val) async{
           for(var value in val){
            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));
                }
              List ids = [];
              for(var value in info){
             ids.add(value.id);
                 }
            QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
           List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
            setState(() {
            this.users = users;
                 });
              });
          }
临时型号:

     class certainrangeusers {
     final String id;
     final double away;
     final GeoPoint userpoint;
      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);

       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'],
       )
        ).toList());

          users1.then((val) async{
           for(var value in val){
            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));
                }
              List ids = [];
              for(var value in info){
             ids.add(value.id);
                 }
            QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
           List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
            setState(() {
            this.users = users;
                 });
              });
          }
变量:

     class certainrangeusers {
     final String id;
     final double away;
     final GeoPoint userpoint;
      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);

       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'],
       )
        ).toList());

          users1.then((val) async{
           for(var value in val){
            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));
                }
              List ids = [];
              for(var value in info){
             ids.add(value.id);
                 }
            QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
           List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
            setState(() {
            this.users = users;
                 });
              });
          }
List info=[];
代码:

     class certainrangeusers {
     final String id;
     final double away;
     final GeoPoint userpoint;
      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);

       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'],
       )
        ).toList());

          users1.then((val) async{
           for(var value in val){
            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));
                }
              List ids = [];
              for(var value in info){
             ids.add(value.id);
                 }
            QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
           List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
            setState(() {
            this.users = users;
                 });
              });
          }
getUsers()异步
{
双半径=0.3;
字符串字段=“地理位置”;
打印(当前用户点经度);
地理点中心=地理点(纬度:currentUser.point.latitude,
经度:currentUser.point.longitude);
var collectionRef=Firestore.instance.collection('user_locations').document(currentUser.pincode)
.collection(“用户、完整地址和地理点”);
this.stream=geo.collection(collectionRef:collectionRef)
.内(中心:中心,半径:半径,字段:字段,模式:假);
未来用户1=stream.first.then((documents)=>documents.map((doc)=>
特定范围用户(
id:doc['userPhone'],
用户点:doc['geopposition']['geopoint'],
)
).toList());
users1.then((val)异步{
for(var中的var值){
信息添加(certainrangeusers)(
//远离:等待Geolocator().distanceBetween(currentUser.point.latitude,currentUser.point.latitude,value.userpoint.latitude,value.userpoint.latitude),
距离:中心距离(纬度:value.userpoint.lation,lng:value.userpoint.longitude),
));
信息排序((a,b)=>a.away.compareTo(b.away));
}
列表ID=[];
用于(信息中的var值){
add(value.id);
}
QuerySnapshot snapshot=await Firestore.instance.collection('users').where('id',其中:ids.getDocuments();
List users=snapshot.documents.map((doc)=>User.fromDocument(doc)).toList();
设置状态(){
this.users=用户;
});
});
}

错误消息告诉您,用于此查询的数组中不能有空值:

Firestore.instance
    .collection('users')
    .where('id', whereIn: ids)
    .getDocuments();

您必须弄清楚为什么
ids
包含空值,并修复它。根据您在此处显示的内容,我们看不出这是从何而来。

出现此错误:“未处理的异常:PlatformException(错误,无效查询。'in'筛选器不能在值数组中包含'null',null)”,如果您有信息要添加到问题中,你可以直接编辑它而不是添加注释。Doug Stevenson完成了…@DougStevenson你能帮我吗?嘿,@DougStevenson,我已经通过在添加的for循环中添加“id:value.id”解决了null问题,但这里需要的是当我再次使用for循环将已排序的文档id存储在“List id”中时,我想按照这个顺序从firestore带来文档,而不是按照存储在firestore中的文档的顺序。嘿,我已经通过在添加的for循环中添加“id:value.id”解决了null问题,但这里需要的是当我再次使用for循环将排序后的文档id存储在“List id”中时,我想把文件按顺序从firestore带来,而不是按顺序从firestore带来。P.S:我花了很长时间才回复,因为我在医院被隔离了。