Firebase Streams with Provider

Tadas Petra
ITNEXT
Published in
3 min readMay 10, 2020

--

There’s been one thing missing from my repertoire during my Firebase journey and that is streams. I knew it was an important topic that I needed to learn, but I wasn’t sure how to manage the stream and everything that comes with that. But then I learned about Provider. As I learned about Provider, I learned about StreamProvider, and finally things started coming together.

So what’s so special about streams?

Reading data on request (as I had been doing), you simple get the data from the server once. If you ever need to update the data, you need to call a function to get the data again. Retrieving data once has a lot of use cases. For example on Instagram, you only want to update the feed when the user pulls down to refresh. But what about cases where you want to see the information update as soon as the information in firebase gets changed. That’s where streams come in. Streams will receive a notification when the piece of data you are “streaming” is changed. It then will update that data within your app, so everything is always up to date. For updating the feed on Instagram, that doesn’t make sense. If you follow a lot of people it would be updating constantly and mess up how far you have scrolled down. But for things like chat messages, you need it to be a stream, so that the messages show up at the right time.

There are two types of streams that I use with Firebase and Provider. Those are Authentication Streams and Firestore Streams.

Streaming Authentication State

For authentication streams, you will need to provide the stream to your whole app, since the whole app will need to make sure your authentication state hasn’t changed. In the example shown in this video, we use google sign in, and we stream that authentication state. Notice, we are not actually streaming it directly from Firebase, we are streaming the local auth state. So this is technically not a Firebase Stream. Why would you stream a local auth state? Let’s say someone steals your phone, but you…

--

--