emit-new-bloc-state-instances

Path
lint/correctness/emit-new-bloc-state-instances
Default
On (recommended)
Origin
Falcon
Domain
bloc
Rule
228 of 512
recommendedbloc

Flags Bloc and Cubit emissions that reuse their current state instance.

Re-emitting the same object, including a cascade that mutates that object, can suppress state updates. Create a new state (for example with copyWith) instead. Checks require a confirmed bloc or flutter_bloc API owner and recognize inherited emit, this.emit, super.emit, and Bloc on handler emitter parameters, including Emitter-typed parameters in tear-off handlers. Local bindings and overridden emit/state members are excluded. Only direct state, this.state, super.state, parentheses and cascades rooted in those reads are checked; arbitrary aliases are not traced.

Invalid

example.dartdart
import 'package:bloc/bloc.dart';

class Snapshot {
  int count = 0;
  Snapshot copyWith() => Snapshot();
}
class Counter extends Cubit<Snapshot> {
  Counter() : super(Snapshot());
  void unchanged() {
    emit(state);
    emit(this.state);
    this.emit(state);
    super.emit((state));
    emit(state..count = 1);
    emit((this.state)..count = 2);
  }
}
class Event {}
class CounterBloc extends Bloc<Event, Snapshot> {
  CounterBloc() : super(Snapshot()) {
    on<Event>((event, emit) {
      emit(state);
      emit(this.state..count = 3);
    });
  }
}
Create a new state instance instead of emitting the current state.
9 void unchanged() {
10 emit(state);
Create a new state instance instead of emitting the current state.
10 emit(state);
11 emit(this.state);
Create a new state instance instead of emitting the current state.
11 emit(this.state);
12 this.emit(state);
Create a new state instance instead of emitting the current state.
12 this.emit(state);
13 super.emit((state));
Create a new state instance instead of emitting the current state.
13 super.emit((state));
14 emit(state..count = 1);
Create a new state instance instead of emitting the current state.
14 emit(state..count = 1);
15 emit((this.state)..count = 2);
Create a new state instance instead of emitting the current state.
21 on<Event>((event, emit) {
22 emit(state);
Create a new state instance instead of emitting the current state.
22 emit(state);
23 emit(this.state..count = 3);

Valid

example.dartdart
import 'package:bloc/bloc.dart';

class Snapshot {
  int count = 0;
  Snapshot copyWith() => Snapshot();
}
class Counter extends Cubit<Snapshot> {
  Counter() : super(Snapshot());
  void changed() {
    emit(Snapshot());
    emit(state.copyWith());
    emit(Snapshot()..count++);
  }
  void shadowedState(Snapshot state) { emit(state); }
  void shadowedEmit(void Function(Snapshot) emit) { emit(state); }
  void nested() {
    void emit(Snapshot value) {}
    emit(this.state);
  }
}
class Event {}
class CounterBloc extends Bloc<Event, Snapshot> {
  CounterBloc() : super(Snapshot()) {
    on<Event>((event, emit) {
      emit(state.copyWith());
      void nested(void Function(Snapshot) emit) { emit(state); }
    });
  }
}
class Unrelated {
  final state = Snapshot();
  void emit(Snapshot value) {}
  void run() { emit(state); }
}

Configure emit-new-bloc-state-instances

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "emit-new-bloc-state-instances": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/emit-new-bloc-state-instances: <reason>
  • exhaustive-casesexhaustive_casesrecommended

    Require switch statements over enum-like classes to cover every constant value.

  • function-always-returns-null

    Flags value functions with a proven explicit null return on every path.

  • function-always-returns-same-value

    Warns when distinct return paths in a function produce the same scalar constant.

  • handle-bloc-event-subclassesbloc

    Warns when a Bloc constructor leaves a concrete event subclass without a handler.

  • hash-and-equalshash_and_equalsrecommended

    Require hashCode and == to be overridden together.

  • implementation-importsimplementation_importsrecommended

    Disallow importing another package's private src/ libraries.

  • implicit-reopenimplicit_reopen

    Flags classes that implicitly reopen a supertype declared in the same library.

  • invalid-case-patternsinvalid_case_patternsrecommended

    Flags pre-3.0 switch case expressions that change meaning or break under Dart 3 patterns.