fold<R> method

R fold<R>(
  1. R onSuccess(
    1. T value
    ),
  2. R onFailure(
    1. String error
    )
)

Transforms the result based on its state using pattern matching.

  • onSuccess: Function to call with the value if this is a success
  • onFailure: Function to call with the error message if this is a failure
  • Returns: The result of whichever function was called

Implementation

R fold<R>(R Function(T value) onSuccess, R Function(String error) onFailure) {
  if (isSuccess) {
    return onSuccess(_value as T);
  } else {
    return onFailure(_error!);
  }
}