unwrapOr method

T unwrapOr(
  1. T defaultValue
)

Returns the success value, or defaultValue if this is an error.

A safe way to extract a value with a guaranteed fallback. This method never throws and always returns a value. Use this when you have a sensible default value that doesn't depend on the error. For defaults computed from the error, use unwrapOrElse instead.

Example:

final value = result.unwrapOr(0); // Returns 0 if error
final name = result.unwrapOr('Unknown'); // Returns 'Unknown' if error

Implementation

T unwrapOr(T defaultValue) => switch (this) {
  Ok(:final value) => value,
  Err() => defaultValue,
};