0

firestore image

I have tried this code but it giving me blank list.

Future getDocs() async {
    QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("users").get();
    for (int i = 0; i < querySnapshot.docs.length; i++) {
      var a = querySnapshot.docs[i];
      print(a.id);
    }
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

If you check the bottom right of your screenshots, it says:

The document does not exist, it will not appear in queries or snapshots.

So while there is a document ID, that is only shown in the console so that you can select the subcollections. There is no actual document data for the selected document ID.

In fact, all the document IDs in your users collection are shown in italic, because there are no actual documents for any of them. And as the message says, that means they won't show up in read operations.


The simplest way to get the user documents too, is to actually create those documents when you also add the subcollection. What fields you add to the document doesn't really matter, but I tend to start with a createdAt timestamp.


Alternatively, you can run a collection group query across one of the subcollections and then for each resulting document look up the parent document, so determine the IDs.

Something like:

QuerySnapshot querySnapshot = await
              FirebaseFirestore.instance.collectionGroup("user_info").get();
                    //  collection group query
for (int i = 0; i < querySnapshot.docs.length; i++) {
  var doc = querySnapshot.docs[i];
  print(doc.id);
  print(doc.reference.parent.parent.id); //  print parent document id
}

Given that you may end up reading way more documents here, I'd consider this a workaround though, and typically only use it to generate the missing data once.


Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807