Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Listen to Firestore count()

Is it possible to listen to count() changes of a Firestore collection?

The example below fetches the current count of unread messages.

I could listen to the complete messages-collection and trigger a call to count() everytime the collection is updated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a more elegant way?

final unreadCount = await FirebaseFirestore.instance
        .collection('messages')
        .where('dateRead', isEqualTo: null)
        .where(Filter.or(Filter("selfUid", isEqualTo: selfUid),
            Filter("partnerUid", isEqualTo: selfUid)))
        .count()
        .get();

print(unreadCount.count);

>Solution :

No. It’s stated pretty clearly in the documentation:

You cannot use aggregation queries with real-time listeners and offline queries. Aggregation queries are only supported via direct server response.

Count, sum, and average are all aggregation queries and all have the same limitation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading