Get Realtime Updates from Cloud Firestore Using Flutter Cubit
If you are reading this blog, I assume you’ve searched for Flutter Cubit, Firestore or realtime data, and you expect to find some clues to subscribing to the realtime data or updates from Cloud Firestore using Cubit.
Unfortunately, it is not simple to expose the data stream for Cloud Firestore to consumers through the Cubit. Of course, you can be hacky - just move the data steams out of Cubit and force the consumers to consume streams from Cubit and Cloud Firestore. But, this approach makes everything complicated.
In fact, there is a simpler approach, and it requires us to swap Cubit for Bloc. Substituting Cubit is easy. You can find more details from Matt Rešetár’s blog.
Once you start using Bloc, in mapEventToState
method, you can get a StreamSubscription
from the listen
method of snapshots
and assign it to the StreamSubscription
of Bloc.
That’s it - all done.