How to get documents in firestore, whose document IDs are stored in the list?, i.e bring only those documents....?

156

Here how to search for all documents from the list id

instead of:

await  Firestore.instance.collection('users').where('id', isEqualTo: 
 val.first.id).getDocuments();

use this:

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

Since you have a list of certainchangeuser, do this to get the ids.

users1.then((val){
  // Get the ids
  List ids = [];
  for (var value in val){
    ids.add(value.id);
  }
  //Then
  QuerySnapshot = await 
    Firestore.instance.collection('users').where('id', 
    whereIn:ids).getDocuments();
....
});
Share:
156
Sai
Author by

Sai

Updated on December 20, 2022

Comments

  • Sai
    Sai over 1 year

    I have a list that contains the document Ids of a particular collection "users", and I want to get documents only present in this list, and these documents are in "users" collection as mention before.

    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_complete_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'],
             )
            ).toList());  
             users1.then((val) async{
            QuerySnapshot snapshot = await  Firestore.instance.collection('users').where('id', isEqualTo: val.first.id).getDocuments();
             List<User> users = snapshot.documents.map((doc) =>  User.fromDocument(doc)).toList();
            setState(() {
             this.users = users;
              });
           });
       }
    

    Firebase Structure: Click here for the firebase structure

    Code In image format:

    Click here for the image

    1. In orange box, "users1" is a list having document IDs...of users 10km users
    2. In the pink box, we r using the value of the list...
    3. In the green box, we need to pass the document ids present in the above list...

    P.s: I am using "val.first.id" just to bring the document present at first in the list........But I need to bring every document present in that list...so that's a point I have struck...

    • Adnan karim
      Adnan karim about 4 years
      can you get the document for the first id?
    • Sai
      Sai about 4 years
      @Adnankarim I was able to get the document for the first Id, i have just uploaded the firebase structure
    • Adnan karim
      Adnan karim about 4 years
      check my answer, hope it helps.
  • Doug Stevenson
    Doug Stevenson about 4 years
    Transactions are for reading then writing documents, not just reading.
  • Sai
    Sai about 4 years
    As i am new to flutter
  • Sai
    Sai about 4 years
    Error: "Unhandled Exception: Invalid argument: Instance of 'certainrangeusers' ", got this error
  • Adnan karim
    Adnan karim about 4 years
    make sure "val" is a list
  • Adnan karim
    Adnan karim about 4 years
    Well this only takes "id's" not list of "certainrangeusers"
  • Sai
    Sai about 4 years
    class certainrangeusers { final String id; certainrangeusers({this.id}); }
  • Sai
    Sai about 4 years
    is the class for the list<certainrangeusers> = [];
  • Adnan karim
    Adnan karim about 4 years
    Yes your are passing an object that contains id, but it only accepts ids like "List ids = ["23","41","23"]
  • Sai
    Sai about 4 years
    Future<List<certainrangeusers>> users1 = stream.first.then( (documents) => documents.map((doc) => certainrangeusers( id: doc['userPhone'], ) )
  • Sai
    Sai about 4 years
    i am doing as above
  • Sai
    Sai about 4 years
    but it only accepts ids like "List ids = ["23","41","23"] , how to do this....considering my scenario
  • Adnan karim
    Adnan karim about 4 years
    @saivaraprasadboggula check the answer
  • Sai
    Sai about 4 years
  • Sai
    Sai about 4 years
    stackoverflow.com/q/61600322/13462594 , is the continuation of the question