unwrapOrNull method

T? unwrapOrNull()

Returns the success value as nullable, or null if this is an error.

A safe way to extract a value when null is an acceptable default for errors. Never throws an exception. Use this for optional extraction without distinguishing between error states.

Warning: If T is nullable, this method cannot distinguish between Ok(null) and Err(...)—both return null. For nullable T types, use toOption for safe extraction, or isOk to check the state first.

Example:

final value = result.unwrapOrNull(); // Nullable<T>
final name = getName().unwrapOrNull() ?? 'Unknown';

Implementation

T? unwrapOrNull() => switch (this) {
  Ok(:final value) => value,
  Err() => null,
};