BlocBuilder and BlocSelector

Learn about BlocBuilder and BlocSelector and how they are used by widgets to listen to state changes.

The BlocBuilder widget

Remember how we used StreamBuilder to listen to BLoC changes when we implemented BLoC ourselves? The BlocBuilder widget is used to simplify and clean up the StreamBuilder to fit the BLoC needs.

Usage

Two parameters are needed for the BlocBuilder widget: bloc and builder. We give the bloc parameter the BLoC object we want to listen to, and every time the state changes, the widget is rebuilt. The BuildContext and state are passed to the builder() function, and it should return the widget that will be rebuilt when the state changes.

Get hands-on with 1200+ tech skills courses.