Problem Statement
What is the purpose of BlocBuilder widget?
Explanation
BlocBuilder listens to a Bloc or Cubit and rebuilds its subtree whenever a new state is emitted, similar to StreamBuilder but specifically designed for flutter_bloc. It takes a builder function receiving context and state, returning the widget tree that should be built for the current state. BlocBuilder handles subscribing to state stream and unsubscribing automatically.
The buildWhen parameter provides fine-grained control over when rebuilds occur by comparing previous and current states, preventing unnecessary rebuilds when state changes don't affect the UI. For example, buildWhen: (previous, current) => previous.count != current.count only rebuilds when count changes, ignoring other property changes.
Use BlocBuilder for declarative UI that reacts to state changes. It's the primary way to consume state from Bloc/Cubit in widgets. For side effects like navigation or showing dialogs in response to state changes, use BlocListener instead. For both rebuilding and side effects, use BlocConsumer which combines BlocBuilder and BlocListener functionality.
