-
Notifications
You must be signed in to change notification settings - Fork 4
BLoC
A Business Logic Component (BLoC) is a chunk of code that helps to manage the state of the app without directly controlling the UI.
Flutter is open-source: anyone can publish a package, so we have lots of BLoC state management options.
You can make a BLoC in Flutter without any third-party libraries, by using a combination of ValueNotifier
and InheritedWidget
.
Provider is the recommended package in Flutter's simple app state management page:
If you are new to Flutter… this is probably the approach you should start with. The provider package is easy to understand and it doesn’t use much code. It also uses concepts that are applicable in every other approach.
The provider package builds off of Flutter's InheritedWidget
to offer new classes that are easier to work with and don't require as much code to set up, including Provider
and Consumer
.
Just like how provider builds on top of default widgets, flutter_bloc builds off of provider widgets to make new classes:
-
Bloc
takes UI events as inputs and then outputs a stream of states that can rebuild widgets with new values. -
Cubit
is similar but more simple: instead of defining events, you create aCubit
object and then use object methods to create new states. -
BlocProvider
andBlocConsumer
were copied from the provider package and then adapted to work withBloc
s andCubit
s.
The same team that published provider decided to mix the letters around and release another package with even more features.
Riverpod incorporates elements from provider, and also other concepts, including code generation and the flutter_hooks package.
click to expand, or maybe just skip this part
provider: I'm a big fan. It makes existing Flutter classes more convenient and easier to use.
riverpod: From what I've read from a few places, this package is powerful but also difficult to learn. The developers have said they're waiting on features like static metaprogramming to be implemented in Dart before riverpod can reach its full potential. All in all, I'm very interested in this package, but I don't think it's a good fit for our project.
flutter_bloc: [Insert a strongly-worded negative statement here.] This package (and its documentation) adheres to the BLoC pattern as closely as possible: UI and business logic should be decoupled, which means that stateful widgets should never be used.
The worst part, in my opinion, is the Bloc
class. "Take a stream of events as input and produce a stream of states as output" sounds wonderful, and I really like the way that Cubit
does it. But in order to make a Bloc
class, you also need to create an additional class declaration for each event.
This is what terms like "boilerplate" and "spaghetti code" were made to describe.
We're using the provider package to simplify app state management.
ValueNotifier
is almost identical to Cubit
, so we can get the same functionality without any extra dependencies!