emit method

Updates the state to the provided state. emit does nothing if the state being emitted is equal to the current state.

To allow for the possibility of notifying listeners of the initial state, emitting a state which is equal to the initial state is allowed as long as it is the first thing emitted by the instance.

Implementation

@protected
@visibleForTesting
@override
void emit(State state) {
  try {
    if (isClosed) {
      throw StateError('Cannot emit new states after calling close');
    }
    if (state == _state && _emitted) return;
    onChange(Change<State>(currentState: this.state, nextState: state));
    _state = state;
    _stateController.add(_state);
    _emitted = true;
  } catch (error, stackTrace) {
    onError(error, stackTrace);
    rethrow;
  }
}