1
votes

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
                    ...

Firestore Subcollection:

documentID passed to the .document() method

1
Add ur firestore - Peter Haddad
@PeterHaddad - final _firestore = Firestore.instance; Sorry about the multiple edits, new to StackOverflow - yesharish
Can we see a screenshot of your firestore dashboard, and the value for documentID? - dshukertjr
@dshukertjr: I've added the images but they are appearing as links as I am a new user on Stack Overflow - yesharish
@yesharish Thank you. That helps a lot! - dshukertjr

1 Answers

1
votes

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!