IntermediateState Management

What is the BLoC pattern in Flutter?

Short Answer

BLoC (Business Logic Component) separates UI from business logic by having the UI dispatch Events into a Bloc, which processes them and emits new States that the UI rebuilds from — all communication happens through a strict, unidirectional stream of Events and States.

A widget never mutates state directly. Instead, it sends an Event (e.g. `IncrementPressed()`) into a Bloc using `context.read<CounterBloc>().add(IncrementPressed())`. The Bloc's internal logic maps that event to a new State (e.g. `CounterState(count: count + 1)`) and emits it. The UI listens via `BlocBuilder` (rebuild on state changes) or `BlocListener` (side effects like navigation/snackbars without rebuilding), so the data flow is always Event → Bloc → State → UI, never the reverse.

This strict separation makes business logic fully testable without any widget tree at all — you can unit test a Bloc by feeding it events and asserting on the emitted states, with no Flutter dependency in the test.

Common Mistakes

  • ×Putting Flutter-specific code (like `BuildContext`) inside a Bloc — it should be pure Dart, completely UI-agnostic.
  • ×Confusing `BlocBuilder` (for rebuilding UI) with `BlocListener` (for one-off side effects) and using the wrong one.