I am trying to query a subcollection from a StreamBuilder but the query returns zero documents, although there is a document in the subcollection. Querying the parent collection returns the correct number of documents and the document ID passed to the subcollection has been verified. What am I missing?
StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('books')
.document(documentID)
.collection('enquiries')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('You have not received any enquiries yet!');
} else {
final numberOfDocs = snapshot.data.documents.length;
print(numberOfDocs); // HELP HERE! returns 0, although a document exists in the subcollection
print(documentID); // returns the expected document ID
...
This is very confusing for beginners in Firestore, but from the screenshot that you have provided, it seems like you actually do not have any documents in the sub-collection.
When the name of the document ID is italicized, it means that that document has sub-collections, but not have the document itself.
This happens when you create a sub-collection of a document and then delete the document later.
Make sure to create some documents in books/kOhVCohNlbQWkGem06RF/enquiries and you should be good to go!