onState<S> method

void onState <S>(
  1. Object target,
  2. dynamic handler(
    1. S
    )
)

Used to attach a handler whenever a state change occurs.

  onState<TodosLoadedState>(this, _updateStats);

It's important to always run removeStateHandlers(this) on dispose.

Implementation

void onState<S>(Object target, Function(S) handler) {
  if (!_stateHandlerMap.containsKey(target.runtimeType)) {
    _stateHandlerMap[target.runtimeType] = [];
  }

  _stateHandlerMap[target.runtimeType].add(
    getStateStream<S>().listen(handler),
  );
}