Search⌘ K
AI Features

BlocListener

Explore the use of BlocListener widget in Flutter BLoC library to trigger functions on state changes. Understand its difference from BlocBuilder, and learn to control when listeners activate using listenWhen. This lesson guides you in managing side effects like showing snackbars based on state updates without rebuilding UI components.

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 ...