I have flutter mobile and flutter web using same firebase firestore. But somehow I can not read all data from web.
final collection = FirebaseFirestore.instance.collection(_allReports);
print('* * * allReports - ${(await collection.get()).docs.length}');
on output 0, while there is a two item showed on above screen shot.
I had some experience, if I create item with random symbols instead of user id, it works. And if try to get reports by providing whole path ‘all_reports/user_id/reports/’ it works also.
Rules:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if isUserAuthenticated();
}
match /all_reports/{document=**} {
allow read: if true;
}
/* Functions */
function isUserAuthenticated() {
return request.auth.uid != null;
}
}
}
Can anyone solve this?
2
Answers
I found the solution! It is easier than I thought.
Look at how your documents are displayed in an italic font: This means that these documents are only present as "container" of one or more sub-collection but that they are not "genuine" documents.
They are shown in the Firestore console (in italic) for you to be able to open the subcollection (i.e.
reports
) but they actually don’t exist, hence the value of0
fordocs.length
(theQuerySnapshot
returned by the query is empty).And this also explains why you get a non empty
QuerySnapshot
when you query the subcollection with the following path:all_reports/user_id/reports/
.If you want to query the
all_reports
collection you need to create the corresponding documents. But are you sure you need to query this collection?Update following your comment:
You can use a Collection Group query.: A Collection Group consists of all collections with the same ID (i.e.
reports
in your case).Note that if you need the user’s uid for each report (i.e. the "fake" parent document), you can use the approach explained in this SO answer, i.e. getting the parent ref of the report document (it is the
reports
subcollection for the user) and then the parent document of this subcollection.