'List names of all documents in subcollection - Firebase Flutter
I am new to Flutter and Firebase. I am trying to get all document Names in a certain collection. Each user has documents which contains todo items.
I already tried it with getting a document snapshot and returning the reference id, but firstly this is a id, not the name, and secondly the id changes on every execution?
Future<String> getLists() async {
DocumentReference doc_ref = FirebaseFirestore.instance
.collection('userTodos')
.doc(userID)
.collection('lists')
.doc();
DocumentSnapshot docSnap = await doc_ref.get();
return docSnap.reference.id;
}
I know that it's not possible to get subcollection on mobile apps, but the names of documents should be possible right?
This is how my Firebase looks like:
I mean, I could create a document which has all document names as items, but this way it would reduce code and would be nicer. Thanks for your help!
UPDATE:
This is my console Output each time the function is called and I print it to the console.
Solution 1:[1]
I have solved it with trial and error: You have to itterate through all documents and then add the id's to a list:
Future<List> getLists() async {
List<String> userLists = [];
CollectionReference col_ref = FirebaseFirestore.instance
.collection('userTodos')
.doc(userID)
.collection('Lists');
QuerySnapshot docSnap = await col_ref.get();
docSnap.docs.forEach((elements) {
userLists.add(elements.id);
});
return userLists;
}
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 |
