emit-new-bloc-state-instances
Warns when an emit
invocation receives the existing state instead of a newly created instance.
Passing existing state objects can create issues when the state is not properly updating.
Example
❌ Bad:
abstract class CounterEvent {}
class CounterDecrementEvent extends CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterDecrementEvent>((event, emit) async {
// LINT: Avoid emitting existing state objects as it can lead to missing state updates. Try creating a new instance instead.
emit(state);
});
}
}
✅ Good:
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterDecrementEvent>((event, emit) async {
emit(state + 1);
emit(state.copyWith(...));
});
}
}