BlocListener
Learn about BlocListener and how it's used by widgets to listen to state changes.
We'll cover the following
The BlocListener
is another commonly used widget when implementing BLoCs using the flutter_bloc
library. With this widget, we can call a function whenever there’s a change in the state we monitor. The main difference between BlocListener
and BlocBuilder
is that BlocListener
doesn’t return a widget, while BlocBuilder
returns a widget that will be rebuilt every time the state changes.
Basic usage
The BlocListener
widget takes a listener()
function and an optional bloc
parameter. The listener()
function is automatically provided with a BuildContext
and a state
, and it doesn’t return anything. This listener()
function is called every time the state changes. We can use it to show a snackbar, make an API call, or perform any other action our app requires.
The widget can also be provided with a child
parameter, which will be a descendant widget of the BlocListener
widget. Note that the widget in the child
parameter will not be rebuilt when the state changes.
Get hands-on with 1200+ tech skills courses.