Skip to main content

handle-bloc-event-subclasses

added in: 1.24.0
Pro+

Warns when a bloc does not handle all event subclasses.

Usually, blocs are expected to handle all event subclasses or the event class itself.

Example

❌ Bad:

abstract class CounterEvent {}

class CounterIncrementEvent extends CounterEvent {}

class CounterDecrementEvent extends CounterEvent {}

class CounterBloc extends Bloc<CounterEvent, int> {
// LINT: This bloc does not handle all event subclasses. Try adding missing event handlers or handle the parent event.
CounterBloc() : super(0) {
on<CounterIncrementEvent>(_handle);
}

Future<void> _handle(CounterEvent event, Emitter<int> emit) async {}
}

✅ Good:

class CorrectBloc extends Bloc<CounterEvent, int> {
CorrectBloc() : super(0) {
on<CounterIncrementEvent>(_handle);
on<CounterDecrementEvent>(_handle);
}

Future<void> _handle(CounterEvent event, Emitter<int> emit) async {}
}

class CorrectBloc extends Bloc<CounterEvent, int> {
CorrectBloc() : super(0) {
on<CounterEvent>(_handle);
}

Future<void> _handle(CounterEvent event, Emitter<int> emit) async {}
}