mayBeMap<R extends Object?> method

R mayBeMap<R extends Object?>({
  1. required R orElse(),
  2. R initialState()?,
  3. R loadingState(
    1. double? progress
    )?,
  4. R succeedState(
    1. T successObject
    )?,
  5. R failedState(
    1. String? failureMessage,
    2. dynamic exception
    )?,
})

Implementation

R mayBeMap<R extends Object?>({
  required R Function() orElse,
  R Function()? initialState,
  R Function(double? progress)? loadingState,
  R Function(T successObject)? succeedState,
  R Function(String? failureMessage, dynamic exception)? failedState,
}) {
  final FourStates<T> state = this;
  return switch (state) {
    InitialState<T>() => initialState == null ? orElse() : initialState(),
    LoadingState<T>() => loadingState == null ? orElse() : loadingState(state.progress),
    SucceedState<T>() => succeedState == null ? orElse() : succeedState(state.successObject),
    FailedState<T>() =>
      failedState == null ? orElse() : failedState(state.failureMessage, state.exceptionObject),
  };
}