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();
....
});
Author by
Sai
Updated on December 20, 2022Comments
-
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:
- In orange box, "users1" is a list having document IDs...of users 10km users
- In the pink box, we r using the value of the list...
- 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 about 4 yearscan you get the document for the first id?
-
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 about 4 yearscheck my answer, hope it helps.
-
Doug Stevenson about 4 yearsTransactions are for reading then writing documents, not just reading.
-
Sai about 4 yearsAs i am new to flutter
-
Sai about 4 yearsError: "Unhandled Exception: Invalid argument: Instance of 'certainrangeusers' ", got this error
-
Adnan karim about 4 yearsmake sure "val" is a list
-
Adnan karim about 4 yearsWell this only takes "id's" not list of "certainrangeusers"
-
Sai about 4 yearsclass certainrangeusers { final String id; certainrangeusers({this.id}); }
-
Sai about 4 yearsis the class for the list<certainrangeusers> = [];
-
Adnan karim about 4 yearsYes your are passing an object that contains id, but it only accepts ids like "List ids = ["23","41","23"]
-
Sai about 4 yearsFuture<List<certainrangeusers>> users1 = stream.first.then( (documents) => documents.map((doc) => certainrangeusers( id: doc['userPhone'], ) )
-
Sai about 4 yearsi am doing as above
-
Sai about 4 yearsbut it only accepts ids like "List ids = ["23","41","23"] , how to do this....considering my scenario
-
Adnan karim about 4 years@saivaraprasadboggula check the answer
-
Sai about 4 yearsLet us continue this discussion in chat.
-
Sai about 4 yearsstackoverflow.com/q/61600322/13462594 , is the continuation of the question