1
votes

I'm building an e-commerce app but when I query firestore to retrieve data I get documents length 0. Data Is present in Firestore. I have no idea what is causing this issue.

I have added all the dependencies.

 Firestore.instance
        .collection('Products')
        .getDocuments().then((value) => print(value.documents.length));

Why this query is not giving me a total number of documents inside Products.

Firestore screenSHot

When I run this Query I get a Correct number of documents inside Fruits.

Firestore.instance
    .collection('Products')
    .document('ABHISHAK FRUITS').collection('Fruits').getDocuments().then((value) => print(value.documents.length));
1
did you delete those documents? - Peter Haddad
No, They Are present in the database. I have tried by adding new documents. The issue is with only parent collection. - Creator
I have deleted them once. - Creator
Thanks for this link. So, now I have to create Those Documents Manually? - Creator

1 Answers

3
votes

The log message is correct - your first query returned no documents.

Notice that IDs of the documents in the console are in italics. This means that the document is not present. The reason why the console is showing these missing documents is because the document ID has a subcollection nested under it, and it's letting you click through to browse them.

Your second query using the subcollection works fine because the subcollection has documents present in it.

The bottom line here is that subcollections are not really "contained" within their parent documents like a traditional computer filesystem. The parents documents can be created or deleted without affecting the any subcollections nested under them.