问题
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
- 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...
回答1:
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();
....
});
回答2:
You can use a transaction for this purpose. Firestore transactions
来源:https://stackoverflow.com/questions/61584880/how-to-get-documents-in-firestore-whose-document-ids-are-stored-in-the-list-i