avoid-duplicate-bloc-event-handlers
- Group
- Correctness
- Path
lint/correctness/avoid-duplicate-bloc-event-handlers- Default
- On (recommended)
- Origin
- Falcon
- Domain
- bloc
- Rule
- 45 of 512
Flags later registrations of the same event type in a Bloc constructor.
Bloc permits only one on<Event> handler per event type. Combine the handlers
instead of registering the same event twice. This rule checks unconditional
constructor registrations on this, including unqualified calls, separately
for each constructor. Deferred callbacks, branches, other receivers, local
on bindings, unresolved generic arguments, opaque mixins and classes
overriding on are excluded. Bloc identity requires package resolution or
a matching bloc/flutter_bloc import and superclass.
Bad
on<Started>((event, emit) {});
on<Started>((event, emit) {}); // A second handler for Started.
Good
on<Started>((event, emit) { initialize(); refresh(); });
Invalid
import 'package:bloc/bloc.dart';
sealed class Event {}
final class Start extends Event {}
final class Stop extends Event {}
final class Reset extends Event {}
class CounterBloc extends Bloc<Event, int> {
CounterBloc() : super(0) {
on<Start>((event, emit) {});
on<Start>((event, emit) {});
this.on<Start>((event, emit) {});
on<Stop>((event, emit) {});
this.on<Stop>((event, emit) {});
on<Reset>((event, emit) {});
on<Reset>((event, emit) {});
}
CounterBloc.named() : super(0) {
on<Start>((event, emit) {});
on<Start>((event, emit) {});
}
} on<Start>((event, emit) {}); on<Start>((event, emit) {}); on<Start>((event, emit) {}); this.on<Start>((event, emit) {}); on<Stop>((event, emit) {}); this.on<Stop>((event, emit) {}); on<Reset>((event, emit) {}); on<Reset>((event, emit) {}); on<Start>((event, emit) {}); on<Start>((event, emit) {});Valid
import 'package:bloc/bloc.dart';
sealed class Event {}
final class Start extends Event {}
final class Stop extends Event {}
class CounterBloc extends Bloc<Event, int> {
CounterBloc() : super(0) {
on<Start>((event, emit) {});
on<Stop>((event, emit) {});
}
CounterBloc.named() : super(0) {
on<Start>((event, emit) {});
}
CounterBloc.other(CounterBloc other) : super(0) {
on<Start>((event, emit) {});
other.on<Start>((event, emit) {});
}
CounterBloc.local(void Function<T>(Object callback) on) : super(0) {
on<Start>((event, emit) {});
on<Start>((event, emit) {});
}
CounterBloc.branch(bool flag) : super(0) {
if (flag) {
on<Start>((event, emit) {});
} else {
on<Start>((event, emit) {});
}
}
}
class Registry {
Registry() {
on<Start>(() {});
on<Start>(() {});
}
void on<T>(Object callback) {}
}
class CustomBloc extends Bloc<Event, int> {
CustomBloc() : super(0) {
on<Start>(() {});
on<Start>(() {});
}
void on<T>(Object callback) {}
}
class InheritedCustomBloc extends CustomBloc {
InheritedCustomBloc() : super() {
on<Start>(() {});
on<Start>(() {});
}
}Configure avoid-duplicate-bloc-event-handlers
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-duplicate-bloc-event-handlers": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-duplicate-bloc-event-handlers: <reason>More correctness rules
- avoid-duplicate-cascades
Flags repeated field or index assignment targets within one cascade.
- avoid-duplicate-collection-elements
Flags repeated scalar literals and stable local reads in list and set literals.
- avoid-duplicate-exports
Flags export directives whose visible names are already covered by another export.
- avoid-duplicate-factories
Flags factories in one type with the same implementation, ignoring factory names and alpha-renaming parameters by position.
- avoid-duplicate-initializers
Flags final/const variables with identical initializers in the same scope.
- avoid-duplicate-map-keysrecommended
Flags repeated literal keys in map literals, including literal spreads and collection
ifentries. - avoid-duplicate-mixins
Flags repeated mixins in a with clause or a same-library superclass chain.
- avoid-duplicate-named-imports
Flags imports of the same library that differ only in their name prefix.