when<R> method Null safety

R when<R>(
  1. {required R success(
    1. Success<T>
    ),
  2. required R failure(
    1. Failure<T>
    ),
  3. required R loading(
    1. Loading<T>
    )}
)

Implementation

R when<R>({
  required R Function(Success<T>) success,
  required R Function(Failure<T>) failure,
  required R Function(Loading<T>) loading,
}) {
  if (this is Success<T>) {
    return success(this as Success<T>);
  }
  if (this is Failure<T>) {
    return failure(this as Failure<T>);
  }
  if (this is Loading<T>) {
    return loading(this as Loading<T>);
  }
  throw new Exception('Should never get here');
}