'How to return document references from firebase collection using dart programing language?
I am using a Restaurant collection to store all restaurants information. The document ID represent a unique restaurant. The document contain more collections (orders, items etc.). The orders collection contain documents that are same as the user UID. Inside the document contain a collection call "All Orders" that saves all the orders of that particular user.
I want to return the Restaurant document refence ids if the user purchased anything from that restaurants. Then return a list of document references type casted to string.
Here is what I have so far:
Future<List<String>> getResDocIDS() async {
List<String> ids = ["none"];
DocumentReference collectionDoc = _firestore.collection("Restaurant").where(
_auth.currentUser!.uid,
isEqualTo: _firestore
.collection("Restaurant")
.doc()
.collection("Orders")
.doc(_auth.currentUser!.uid))
return ids;
}
Solution 1:[1]
Firestore queries can only have conditions on data that is returned by that query. So there's no way to run a query on Restaurant with a condition on each restaurant's Orders subcollection.
If you want to search across all Orders subcollections, you'll need to use a collection group query. Then from the resulting orders, you can determine the parent restaurant document reference, and if needed load those documents.
Alternatively, you can add a orderingUsers field to each restaurant where you track the UID of users who ordered from that restaurant, but you'll have to keep an eye on the size of the document in that case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
