Using Async For Your First Flutter State Management Step
--
One of the beefs I have with the Flutter SDK Team and their docs is at times certain areas do not take the opportunity to use the subject they are detailing to teach. For example, in the doc area of async programming that while they could use async to talk about basic pre-steps of state management; they in fact do not. I guess it’s my opportunity to change that aspect.
So, what are we talking about? This is what we normally see, the MVC diagram:
Now, I am not talking about modifying behavior. Instead, what we are talking about is modifying HOW THE BEHAVIOR IS IMPLEMENTED. One of the places where we could make an impact is in how we decouple the MODEL from the VIEW and the Controller.
Even though we do no use legacy MVC (For example, BLoC is in fact MVP), we are still talking about how the MODEL communicates with the rest of the components. While we want to decouple the model per communicating with other components, we cannot use an event bus as it would couple each component to the Model via the event bus.
This is where the dart async package has a role to play, as it is in fact the training-wheels to stream programming. So let’s work through an example.
What the Dart async package allows us to do through streaming is somewhat decoupled callbacks, in that the communication between the callback and another component is in fact decoupled as a stream. In the flutter template skeleton app, we already have a hint to that in that we could grab the ThemeMode change as an event. The package we will use to be able to use the Dart async package to do events is the Event Package: