Flutter firestore snapshot always null or empty list

Viewed 3608

i am trying to fetch firestore snapshot but it always return null , i don't know where is the problem ..

please see the image here

my rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

my code 1:

await Firestore.instance.document("myDB/EG/EG/Cairo Governorate").collection("Alexandria Governorate").limit(10).getDocuments().then((snapshot)async{
print(snapshot.documents);//[]
});

my code 2:

await Firestore.instance.document("myDB/EG/EG/Cairo Governorate/Alexandria Governorate").get().then((snapshot)async{
print(snapshot.data);//null
});

Update: how this document does not exist ?

2 Answers

In your first bit of code, an empty array means that your query didn't find any documents in the named collection.

In your second bit of code, a null for snapshot.data means that the documented requested doesn't exist.

In both cases, since we can't see your data and verify that it exists, there's nothing else that can be said. This has nothing to do with security rules, as your queries would fail completely if they were rejected by rules. I suspect the names in your code simply don't match what's in your database.

In Firebase docs regarding Delete Douments there is warning that when you delete document it does not delete its subcollections. This is why those docs are marked with this description. However subcollections should be still available.

In your both samples of code you use "Alexandria Governorate" as document while guessing from the screenshot its a collection.

I do not have playground to test it however it should go like this:

await Firestore.instance.collection("myDB/EG/EG/Cairo Governorate/Alexandria Governorate").limit(10).getDocuments().then((snapshot)async{
print(snapshot.documents);//[]
});

and like this:

await Firestore.instance.document("myDB/EG/EG/Cairo Governorate/Alexandria Governorate/<EXISTING DOCUMENT ID>").get().then((snapshot)async{
print(snapshot.data);//null
});

Such queries might be successful if any of documents still exist. As all documents in your screenshots are in Italic font I suppose that only references to some empty subcollection left. So you will not be able to query anything.

I hope it will help!

Related