emit-new-bloc-state-instances
- Group
- Correctness
- Path
lint/correctness/emit-new-bloc-state-instances- Default
- On (recommended)
- Origin
- Falcon
- Domain
- bloc
- Rule
- 228 of 512
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
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);
});
}
} void unchanged() { emit(state); emit(state); emit(this.state); emit(this.state); this.emit(state); this.emit(state); super.emit((state)); super.emit((state)); emit(state..count = 1); emit(state..count = 1); emit((this.state)..count = 2); on<Event>((event, emit) { emit(state); emit(state); emit(this.state..count = 3);Valid
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.
{
"linter": {
"rules": {
"correctness": {
"emit-new-bloc-state-instances": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/emit-new-bloc-state-instances: <reason>More correctness rules
- exhaustive-cases
exhaustive_casesrecommendedRequire 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-equals
hash_and_equalsrecommendedRequire
hashCodeand==to be overridden together. - implementation-imports
implementation_importsrecommendedDisallow importing another package's private
src/libraries. - implicit-reopen
implicit_reopenFlags classes that implicitly reopen a supertype declared in the same library.
- invalid-case-patterns
invalid_case_patternsrecommendedFlags pre-3.0 switch case expressions that change meaning or break under Dart 3 patterns.