handle-bloc-event-subclasses
Warns when a bloc does not handle all event subclasses.
Usually, blocs are expected to handle all event subclasses or the event class itself.
⚙️ Config
Set additional-methods
(default is empty) to add additional methods used for listening to bloc events.
analysis_options.yaml
dart_code_metrics:
rules:
- handle-bloc-event-subclasses:
additional-methods:
- customOn
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 {}
}