My use of the classic (or "vintage") BLoC pattern (not to be confused with Felix Angelov's great bloc
library) has evolved over the years. I do no use dedicated sinks for input and dedicated streams for output. Rather, I directly use rxdart
's excellent BehaviorSubject
directly. BehaviorSubject
implements ValueStream
, which itself implements Stream
, so I found that I could reduce boilerplate a lot by doing this. Values can be directly added to, and read from, a BehaviorSubject
. I then use provider
to pass my services (I don't really think of them as "bloc"s any more) through my app. This gist provides a real example of how I currently use this pattern.
prefs_service.dart
is where the service is defined.main.dart
shows how to initialize the service.app.dart
shows how I useMultiProvider
to pass down my service. I always useMultiProvider
rather than one single provider to allow for more services. I listen to theBehaviorSubject
in my service to set theThemeMode
of myMaterialApp
.provided.dart
shows how I use a mixin as a shortcut to my service.theme_mode_switcher.dart
shows how I use the mixin to get the service and update the value of theBehaviorSubject
on user selection.
Disclaimer: I do not claim that this is the “right” way to do state management (there’s no such thing) and my style evolves over time. Additionally, there is nothing wrong with Felix's bloc
ecosystem. Felix is great, he does amazing work for the Flutter community, and his work is of the highest caliber. I have nothing but respect for him. The discussion over the nature of BLoC pattern vs. his bloc
library that pops up every now and then is not a criticism of him or his work. I hope very much that no one confuses it as such.