dispatch method

  1. @mustCallSuper
void dispatch(
  1. StoreAction action
)

Dispatch an action into the store.

If the action is an StoreAction<State>, the action is executed, and each emitted state will replace the current state.

If the action is an StoreAction<T>, then a registered mapper will be used to map the action and execute it as an StoreAction<State>.

The onDispatchedAction is called before the StoreAction<State> is executed.

The onStateChanged method is called after each state update.

The onDispatchFailed method is called if an exception is thrown during action execution.

Implementation

@mustCallSuper
void dispatch(StoreAction action) async {
  if (action.stateType == State) {
    try {
      onDispatchedAction(action);
      await for (var newState
          in action.execute(() => state, dispatch, _serviceLocator)) {
        state = newState;
        onStateChanged(state);
      }
    } catch (error, stackTrace) {
      onDispatchFailed(action, error, stackTrace);
    }
  } else {
    final mapper = _getMapper(action.stateType);
    dispatch(MappedStoreAction<State>(mapper, action));
  }
}