flutter_bloc
package integrate with both Cubit
and Bloc
instances.BlocBuilder
to hook up a CounterPage
widget to a CounterCubit
.CounterPage
widget knows nothing about what happens when a user taps the buttons. The widget simply tells the CounterCubit
that the user has pressed either the increment or decrement button.cubit
and a builder
function. BlocBuilder
handles building the widget in response to new states. BlocBuilder
is very similar to StreamBuilder
but has a more simple API to reduce the amount of boilerplate code needed. The builder
function will potentially be called many times and should be a pure function that returns a widget in response to the state.BlocListener
if you want to 'do' anything in response to state changes such as navigation, showing a dialog, etc..BlocBuilder
will automatically perform a lookup using BlocProvider
and the current BuildContext
.BlocProvider
and the current BuildContext
.builder
function is called an optional buildWhen
can be provided. buildWhen
takes the previous cubit state and current cubit state and returns a boolean. If buildWhen
returns true, builder
will be called with state
and the widget will rebuild. If buildWhen
returns false, builder
will not be called with state
and no rebuild will occur.BlocProvider.of<T>(context)
. It is used as a dependency injection (DI) widget so that a single instance of a cubit can be provided to multiple widgets within a subtree.BlocProvider
should be used to create new cubits which will be made available to the rest of the subtree. In this case, since BlocProvider
is responsible for creating the cubit, it will automatically handle closing it.create
will get executed when the cubit is looked up via BlocProvider.of<BlocA>(context)
.create
to be run immediately, lazy
can be set to false
. Paperless 3 0 1 – digital documents manager salary.BlocProvider
can be used to provide an existing cubit to a new portion of the widget tree. This will be most commonly used when an existing cubit
needs to be made available to a new route. In this case, BlocProvider
will not automatically close the cubit since it did not create it.ChildA
, or ScreenA
we can retrieve BlocA
with:BlocProvider
widgets into one.MultiBlocProvider
improves the readability and eliminates the need to nest multiple BlocProviders
.By using MultiBlocProvider
we can go from:BlocWidgetListener
and an optional cubit
and invokes the listener
in response to state changes in the cubit. It should be used for functionality that needs to occur once per state change such as navigation, showing a SnackBar
, showing a Dialog
, etc..listener
is only called once for each state change (NOT including the initial state) unlike builder
in BlocBuilder
and is a void
function.BlocListener
will automatically perform a lookup using BlocProvider
and the current BuildContext
.BlocProvider
and the current BuildContext
.listener
function is called an optional listenWhen
can be provided. listenWhen
takes the previous cubit state and current cubit state and returns a boolean. If listenWhen
returns true, listener
will be called with state
. If listenWhen
returns false, listener
will not be called with state
.BlocListener
widgets into one.MultiBlocListener
improves the readability and eliminates the need to nest multiple BlocListeners
.By using MultiBlocListener
we can go from:builder
and listener
in order react to new states. BlocConsumer
is analogous to a nested BlocListener
and BlocBuilder
but reduces the amount of boilerplate needed. BlocConsumer
should only be used when it is necessary to both rebuild UI and execute other reactions to state changes in the cubit
. BlocConsumer
takes a required BlocWidgetBuilder
and BlocWidgetListener
and an optional cubit
, BlocBuilderCondition
, and BlocListenerCondition
.cubit
parameter is omitted, BlocConsumer
will automatically perform a lookup usingBlocProvider
and the current BuildContext
.listenWhen
and buildWhen
can be implemented for more granular control over when listener
and builder
are called. The listenWhen
and buildWhen
will be invoked on each cubit
state
change. They each take the previous state
and current state
and must return a bool
which determines whether or not the builder
and/or listener
function will be invoked. The previous state
will be initialized to the state
of the cubit
when the BlocConsumer
is initialized. listenWhen
and buildWhen
are optional and if they aren't implemented, they will default to true
.RepositoryProvider.of<T>(context)
. It is used as a dependency injection (DI) widget so that a single instance of a repository can be provided to multiple widgets within a subtree. BlocProvider
should be used to provide blocs whereas RepositoryProvider
should only be used for repositories.ChildA
we can retrieve the Repository
instance with:RepositoryProvider
widgets into one.MultiRepositoryProvider
improves the readability and eliminates the need to nest multiple RepositoryProvider
.By using MultiRepositoryProvider
we can go from:CounterBloc
to implement the classic Flutter Counter app.bloc
and flutter_bloc
packages to implement form validation.bloc
to a Stream
and update the UI in response to data from the Stream
.bloc
and flutter_bloc
packages to implement an infinite scrolling list.bloc
and flutter_bloc
packages to implement a Login Flow.bloc
and flutter_bloc
packages to implement login via Firebase.bloc
and flutter_bloc
packages.bloc
and flutter_bloc
packages. The app uses a RefreshIndicator
to implement 'pull-to-refresh' as well as dynamic theming.bloc
and flutter_bloc
packages.bloc
and flutter_bloc
packages.bloc
and flutter_bloc
packages that integrates with cloud firestore.bloc
and flutter_bloc
packages based on flutter samples.bloc
and flutter_bloc
packages to implement a dynamic form which pulls data from a repository.