avoid-emit-outside-bloc
- Group
- Correctness
- Path
lint/correctness/avoid-emit-outside-bloc- Default
- On (recommended)
- Origin
- Falcon
- Domain
- bloc
- Rule
- 58 of 512
Flags external calls and tear-offs of a Bloc or Cubit's emit method.
External code should dispatch Bloc events or call a Cubit's public domain
methods instead of bypassing its state-transition logic. Receivers must be
confirmed through bloc/flutter_bloc package identity or an imported API
superclass. Ordinary and null-aware method calls, tear-offs and cascade
sections are checked. Emission inside the owning class through this,
super, or a receiver of that owner type is allowed; extensions are outside
the owner. Unknown/dynamic receivers and unrelated emit methods are not
reported; no class-name suffix is assumed.
Invalid
import 'package:flutter_bloc/flutter_bloc.dart';
class Counter extends Cubit<int> {
Counter() : super(0);
}
class Event {}
class CounterBloc extends Bloc<Event, int> {
CounterBloc() : super(0);
}
void update(Counter counter, Counter? optional, CounterBloc bloc) {
counter.emit(1);
optional?.emit(2);
bloc.emit(3);
counter..emit(4);
counter..emit(5)..emit(6);
}
class Presenter {
final Counter counter;
Presenter(this.counter);
void refresh() { counter.emit(7); }
}void update(Counter counter, Counter? optional, CounterBloc bloc) { counter.emit(1); counter.emit(1); optional?.emit(2); optional?.emit(2); bloc.emit(3); bloc.emit(3); counter..emit(4); counter..emit(4); counter..emit(5)..emit(6); counter..emit(4); counter..emit(5)..emit(6); Presenter(this.counter); void refresh() { counter.emit(7); }Valid
import 'package:bloc/bloc.dart';
class Counter extends Cubit<int> {
Counter() : super(0);
void increment() { emit(state + 1); }
void explicit() { this.emit(1); }
void inherited() { super.emit(2); }
void cascade() { this..emit(3); }
void internal(Counter other) { other.emit(4); }
}
class Other {
void emit(int value) {}
}
void unrelated(Other other, dynamic unknown, void Function(int) emit) {
other.emit(1);
unknown.emit(2);
emit(3);
other..emit(4);
}
void publicApi(Counter counter) { counter.increment(); }Configure avoid-emit-outside-bloc
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-emit-outside-bloc": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-emit-outside-bloc: <reason>More correctness rules
- avoid-empty-setstaterecommendedflutter
Avoid setState callbacks with empty block bodies.
- avoid-empty-test-groupstest
Reports inline framework groups without any test registrations.
- avoid-global-state
avoid-global-stateDisallow mutable global and static state.
- avoid-hooks-outside-buildrecommendedflutter_hooks
Flags confirmed Flutter hooks outside hook builds, custom hooks, and HookBuilder/HookConsumer builders.
- avoid-incorrect-image-opacityflutter
Flags Opacity directly wrapping a Flutter Image, including named constructors.
- avoid-inherited-widget-in-initstaterecommendedflutter
Avoid inherited-widget subscriptions during initState.
- avoid-instantiating-in-value-providerprovider
Value providers borrow existing instances; newly constructed values need create.
- avoid-late-contextrecommendedflutter
Avoid storing
BuildContextin late fields or reading it in late initializers.