avoid-emit-outside-bloc

Path
lint/correctness/avoid-emit-outside-bloc
Default
On (recommended)
Origin
Falcon
Domain
bloc
Rule
58 of 512
recommendedbloc

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

example.dartdart
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); }
}
Do not emit state outside its Bloc or Cubit; use its events or public methods.
10void update(Counter counter, Counter? optional, CounterBloc bloc) {
11 counter.emit(1);
Do not emit state outside its Bloc or Cubit; use its events or public methods.
11 counter.emit(1);
12 optional?.emit(2);
Do not emit state outside its Bloc or Cubit; use its events or public methods.
12 optional?.emit(2);
13 bloc.emit(3);
Do not emit state outside its Bloc or Cubit; use its events or public methods.
13 bloc.emit(3);
14 counter..emit(4);
Do not emit state outside its Bloc or Cubit; use its events or public methods.
14 counter..emit(4);
15 counter..emit(5)..emit(6);
Do not emit state outside its Bloc or Cubit; use its events or public methods.
14 counter..emit(4);
15 counter..emit(5)..emit(6);
Do not emit state outside its Bloc or Cubit; use its events or public methods.
19 Presenter(this.counter);
20 void refresh() { counter.emit(7); }

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-emit-outside-bloc": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-emit-outside-bloc: <reason>