Sub topic 8
From Cubit to Bloc: events and `on<Event>` handlers
Assume you know Dart classes, generics, null safety, async and await, Flutter widgets, and the basic Cubit pattern. A Cubit changes state when one of its public methods calls emit. A Bloc adds an event layer between the UI and the state change.
An event is an object that describes something that happened or something the user requested. The UI adds an event to the Bloc with add. The Bloc receives it, runs a registered handler, and emits a new state.
Define events as a public input
Each event should represent one meaningful action. Events can carry data, such as an item ID or a search query. Dart 3 sealed classes make the event family explicit.
import 'package:flutter_bloc/flutter_bloc.dart';
sealed class CounterEvent {
const CounterEvent();
}
final class CounterIncremented extends CounterEvent {
const CounterIncremented();
}
final class CounterDecremented extends CounterEvent {
const CounterDecremented();
}
final class CounterAdded extends CounterEvent {
const CounterAdded(this.amount);
final int amount;
}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterIncremented>((event, emit) {
emit(state + 1);
});
on<CounterDecremented>((event, emit) {
emit(state - 1);
});
on<CounterAdded>((event, emit) {
emit(state + event.amount);
});
}
}
// In a widget:
// context.read<CounterBloc>().add(const CounterIncremented());
// context.read<CounterBloc>().add(const CounterAdded(5));Bloc<CounterEvent, int> means that this Bloc accepts CounterEvent objects and produces int states. The constructor passes the initial state, 0, to super.
The on<T> method registers a handler for events of type T. A handler receives two values:
event, which contains the input dataemit, which produces a new state
The handler can be synchronous or asynchronous. For example, a login event might await a repository call before emitting a success or failure state.
Cubit methods versus Bloc events
A Cubit exposes actions as methods:
cubit.increment();A Bloc exposes actions as event objects:
bloc.add(const CounterIncremented());Both approaches separate business logic from widgets. Cubit is usually smaller and direct. Bloc makes each input explicit and gives the event a name, data, and lifecycle that can be logged or tested.
| Use a Cubit when | Use a Bloc when |
|---|---|
| The state changes through a small set of direct methods | Many event types can change the same state |
| The action flow is simple | You need explicit, traceable user events |
| There is little event ordering or concurrency logic | Events need different handling or concurrency policies |
A common error is registering a handler for the base event type and then expecting separate behavior for every subclass. Register one on<SpecificEvent> handler for each event type that needs distinct behavior. Another error is adding events to a Bloc after it has been closed. Close the Bloc with its provider or before the object is discarded.
Exercise
What does on<CounterAdded> do, and where does the value passed to CounterAdded(5) come from?
Show answer
It registers the handler for CounterAdded events. The value 5 is stored in the event's amount field and is read inside the handler.