when<TReturn> method

TReturn when<TReturn>({
  1. required TReturn onState(
    1. S state
    ),
  2. TReturn onLoading(
    1. bool isLoading
    )?,
  3. TReturn onError(
    1. dynamic error
    )?,
})
inherited

Represents a value of one of three mapped possibilities.

EXAMPLE:

int result = store.when<int>(
                onState: (state) => state,
                onLoading: () => 0,
                onError: (error) => -1,
            );

Implementation

TReturn when<TReturn>({
  required TReturn Function(State state) onState,
  TReturn Function(bool isLoading)? onLoading,
  TReturn Function(dynamic error)? onError,
}) {
  if (triple.event == TripleEvent.loading && onLoading != null && triple.isLoading) {
    return onLoading(triple.isLoading);
  } else if (triple.event == TripleEvent.error && onError != null) {
    return onError(triple.error);
  } else {
    return onState(triple.state);
  }
}