SideEffects<T>.onOrElse constructor

SideEffects<T>.onOrElse({
  1. void onWaiting()?,
  2. void onError(
    1. dynamic err,
    2. VoidCallback refresh
    )?,
  3. void onData(
    1. T data
    )?,
  4. required void orElse(
    1. T data
    ),
  5. void initState()?,
  6. void dispose()?,
})

Handle the three state status with one required fallback callback.

Implementation

factory SideEffects.onOrElse({
  void Function()? onWaiting,
  void Function(dynamic err, VoidCallback refresh)? onError,
  void Function(T data)? onData,
  required void Function(T data) orElse,
  void Function()? initState,
  void Function()? dispose,
}) {
  return SideEffects(
    initState: initState,
    dispose: dispose,
    onSetState: (snap) {
      if (snap.isWaiting && onWaiting != null) {
        onWaiting();
        return;
      }

      if (snap.hasError && onError != null) {
        onError(snap.snapError!.error, snap.snapError!.refresher);

        return;
      }

      if (snap.hasData && onData != null) {
        onData(snap.data as T);
        return;
      }
      orElse(snap.data as T);
    },
  );
}