Problem Statement
When should you use BlocListener instead of BlocBuilder?
Explanation
BlocListener is for performing side effects in response to state changes without rebuilding UI - things like navigation, showing dialogs, displaying snackbars, or triggering animations. It listens to state changes and calls a listener function but doesn't rebuild its child, unlike BlocBuilder which rebuilds. Use BlocListener when state changes require actions beyond just updating UI.
Common use case: listening for error states to show error dialogs, success states to navigate to next screen, or loading states to show loading indicators separately from main UI. The listenWhen parameter works like buildWhen, allowing you to specify which state changes should trigger the listener, preventing side effects from executing on every state change.
BlocListener should wrap the widget subtree where side effects make sense. For example, place it near Scaffold for showing SnackBars or near Navigator for navigation logic. BlocConsumer combines BlocBuilder and BlocListener when you need both - rebuild UI and perform side effects. Never perform side effects in BlocBuilder as it can cause issues during rebuilds.
