Simple State Management in Flutter using Streams

Jakub Neander
Flutter Community
Published in
5 min readAug 7, 2020

--

The first question, that probably comes to your mind after reading the title, is: why should I bother about another state management solution?

The answer is: using streams for state management is simpler.

Imagine your Flutter application needs to persist data. Putting it into a database is a common solution. I’d even argue that most Flutter applications need some persistence layer. In many cases, you will probably use Firebase. The offering is good and tightly integrated with Flutter. Firebase API is stream-based (which may be a surprise to some). If you use Firebase, a popular database approach in Flutter, you end up using streams to manage state anyway.

Let’s imagine another scenario. You are interacting with an external API. Let’s say there’s an endpoint for searching — something like Algolia API. It’s a good idea to use streams here too. You may need to control the order of the request-response cycle using something like switchMap, or you may need to give a timing buffer for queries being sent to that endpoint using debounce , both stream-based solutions from RxDart. We end up with streams again, because they provide an optimal solution in such a scenario.

In that case, why don’t we use streams everywhere? whether it’s an external interaction with a database / API, or an internal…

--

--

Jakub Neander
Flutter Community

I teach software development on YouTube as Zaiste Programming. I run a software company in Paris, France.