Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

flutter - How to sort particular items in model that represent firebase documents of particular collection? (P.S: click below for the well answered question)

Click here for the previous version well solved question by me and others

I want to sort the items in the model which represents the firebase documents of particular collections.Also this model contains the document Ids of the users(which will be sorted according to the distance) of the collection "users" and i will bring those documents only from "users" collection. I have calculated the distances from the current-user to the other users and i want bring the nearest users for this user. I have tried following code:

ScreenShot: [In the pink box area, whole calculation is done and assigned and documents brought from firebase] [In the pink box area, whole calculation is done and assigned and documents brought from firebase]2

Got Error:

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

Detailed Code:

Temp model:

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

       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;
                 });
              });
          }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...