Problem description
Two consecutive calls to addEvent result in only one response, and the second event cannot be responded to.
@override Stream<SomeState> mapEventToState(SomeEvent event) async*{ if(event is InCreaseEvent){ state.num ++; yield state; }}Copy the code
someBloc.add(InCreaseEvent());
someBloc.add(InCreaseEvent());
Copy the code
Cause analysis,
Bloc inherited from Cubit. According to the source code of Cubit, we made a judgment during status update. If the received newState and currentState are the same object, we will return directly without responding to this status change.
Treatment:
Each State class must have a copy() method that produces a copy of the State object. Each time you edit the contents of the state field, yield the copy, ensuring that each time you yield the new object.
class SomeBloc extends Bloc<SomeEvent, SomeState>{ SomeState _currentState; SomeBloc(SomeState initialState) : super(initialState){ _currentState = initialState; } @override Stream<SomeState> mapEventToState(SomeEvent event) async*{ if(event is InCreaseEvent){ _currentState.num ++; Yield _currentState.copywith (); } } } class SomeState{ int num; SomeState(this.num); SomeState copyWith(){return SomeState(num); } } abstract class SomeEvent{} class InCreaseEvent extends SomeEvent{}Copy the code
2. Override the get method using Equatable State to inherit Equatable