Sub topic 9
Choosing between Cubit and Bloc
Assume you understand Dart classes, asynchronous code, immutable state, and how BlocProvider exposes a state object to Flutter widgets. Both Cubit and Bloc separate state changes from the UI. The main difference is how those changes begin:
- A
Cubitexposes methods such asincrement()orload(). - A
Blocreceives events such asCounterIncrementedorTodosRequested, then handles those events.
Both publish states through a stream. Both work with BlocBuilder, BlocListener, testing tools, and repositories.
Cubit: direct commands
A Cubit is usually the simplest choice. A method receives the required arguments, performs work, and calls emit with the next state.
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
void decrement() => emit(state - 1);
}The caller describes an action directly:
context.read<CounterCubit>().increment();This style has little ceremony and is easy to read. It works well for local screens, forms, filters, and features with a small number of user actions.
A Cubit does not automatically record why a state changed. A log may show that the count became 1, but not whether a button, shortcut, or restore operation caused it. You can add logging yourself when that distinction matters.
Bloc: events describe intent
A Bloc adds an event layer. Callers add events, and handlers translate those events into states.
sealed class CounterEvent {}
final class IncrementPressed extends CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<IncrementPressed>((event, emit) {
emit(state + 1);
});
}
}
// context.read<CounterBloc>().add(IncrementPressed());Events provide an explicit history of intent. This helps when several sources can trigger the same transition, or when a feature has complex asynchronous workflows. Event handlers also support concurrency policies such as restartable() and droppable().
The extra types and handlers make a Bloc more verbose. That cost is useful only when the event model improves understanding or control.
Decision criteria
| Choose Cubit when | Choose Bloc when |
|---|---|
| The feature has straightforward commands | Events need explicit names and history |
| Minimal code improves readability | Several event sources share handlers |
| State transitions are mostly synchronous | Async events can overlap or require transformers |
| The team does not need event-level tracing | Auditing and debugging intent matter |
A Cubit can evolve into a Bloc without changing the widget’s general structure. The provider and state builder still consume a state stream. The main change is replacing method calls with event objects and handlers.
Common errors
Do not put network calls in widget callbacks when the operation belongs to the feature’s business logic. Call a Cubit method or add a Bloc event instead.
Do not treat Bloc as a replacement for immutable state. A Bloc still needs clear state fields, equality, and predictable transitions.
Be careful when multiple events can run at once. A slow earlier request may finish after a newer request and overwrite its result. Use an event transformer when the feature requires cancellation, queuing, or ignoring duplicate work.
Exercise
A search screen must cancel an older request whenever the user enters a newer query. Which choice is a strong default, and why?
Show answer
Use a Bloc with a restartable event transformer. Search queries become explicit events, and the transformer prevents an older request from replacing newer results.