Reputation: 390
I've been searching high and low. Hopefully someone can help!
firestore setup: usersCollection -> list of userIDs -> itemsCollection -> list of itemIDs -> item_name and etc
A lot of the examples that I was able to find, were targeting a specific userID. What I want is to load all itemIDs from all userIDs. Therefore:
I do not need it to be displayed like the above, I want to display all the items(eventually use the userID to manipulate data). Some were suggesting nested streamers but that would use a lot of power (I do not like the idea)
Then I was thinking of using some sort of 2D for loop or I also saw a for each loop. I believe these would be more suitable....
I know how to get the list of users but then I will need to get the list of items per user... how do I get that portion!? currently I am trying to display the information in a listview with tiles, but right now I just need to know how I can access the list of items.
I tried doing this.
List listUsers = await FirebaseFirestore.instance
.collection("users")
.get();
for (int i = 0; i < listUsers.length; i++) {
FirebaseFirestore.instance
.collection("users")
.doc(listUsers[i].documentID.toString())
.collection("items")
.snapshots();
}
but we can't forget that there is a list of items too So....
List listUsers = await FirebaseFirestore.instance
.collection("users")
.get();
List listItems = await FirebaseFirestore.instance
.collection("items")
.get();
for (int i = 0; i < listUsers.length; i++) {
for (int j = 0; j < listItems.length; j++) {
FirebaseFirestore.instance
.collection("users")
.doc(listUsers[i].documentID.toString())
.collection("items")
.doc(listItems[j].documentID.toString())
.snapshots();
}
????????????? please help...
============== EDIT ===============================
ANSWER
List listItems = await FirebaseFirestore.instance
.collectionGroup('items')
.get()
.then((value) => value.docs);
and you access the list item and its value like this:
print('OMG------------ ${listItems[0].documentID.toString()}'); //document ID
print('OMG------------ ${listItems[0].data()['item_name']}'); // specific value within collection
Upvotes: 0
Views: 177
Reputation: 4272
If I understand correctly this looks like example of collectionGroup
that consists of all collections with the same ID. You can check the general concept on the Firebase documentation.
Of course there is as well Flutter implementation of this feature. You can find it documentation on pub.dev.
Upvotes: 1