0
votes

I have an array or list of name "A", in which there are document Ids. Now I want to bring documents from cloud firestore, in the order in which their document Ids are stored in the list "A".

"whereIn:" Only fetches documents in the order they are in collection of cloud firestore, but i want to fetch in order as mentioned in the list.

Also, I am using ".getDocuments()", in order to fetch data,So please mention, if there any other efficient way of fetching data,which supports offline,caching.As, there can be some 200 to 400 documents.

Code:

    List A;//Not empty :), contains ordered document ids of the collection

   QuerySnapshot snapshot = await someCollectionRef.where('/*somefieldname*/', whereIn: A).getDocuments();

    List<Users> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();//Here converting that snapshot data into list

please mention, if there is any other efficient way of fetching data as I am here using ".getDocuments()",which supports offline,caching.As, there may be some 200 to 400 documents.

Thanks for the help in advance :)

1
The order in which the documents are fetched doesn't seem to make a big difference. You can just display them in the order you want after you fetch them. You already have the order in list A. - Doug Stevenson
@DougStevenson, that's the thing, i really have no idea how to do display them in that order of the list, so t i have thought that it's better to bring them in the order present in the list, rather than sorting them... - sai vara prasad boggula
@DougStevenson, please help me out here sir. - sai vara prasad boggula
It's not that hard to sort small amounts of data in client code. If you're having problems with that, consider asking a new question with the code that's not working the way you expect. - Doug Stevenson
@DougStevenson, i don't have small data, i have almost 300 documents.Sir, i have sorted the data,i.e document Id's, but I feel hard to fetch that those documents in that order. :{ - sai vara prasad boggula

1 Answers

0
votes

this should do what you want, just change the element you want to compare in the data document:

querySnapshot.documents.sort((a, b) {
  return a.data['id'].compareTo(b.data['id']);
});

in the code above, i'm comparing the ID of these docs.