when<R> method

R when<R>({
  1. required R data(
    1. T data
    ),
  2. required R error(
    1. Object,
    2. StackTrace?,
    3. T? previous
    ),
  3. required R loading(
    1. T? previous
    ),
  4. required R nothing(),
})

Implementation

R when<R>({
  required R Function(T data) data,
  required R Function(Object, StackTrace?, T? previous) error,
  required R Function(T? previous) loading,
  required R Function() nothing,
}) {
  if (hasError) {
    return error(this.error!, stackTrace, this.data);
  }

  if (connectionState == ConnectionState.active ||
      connectionState == ConnectionState.waiting) {
    return loading(this.data);
  }

  if (hasData) {
    return data(this.data!);
  }

  return nothing();
}