toNullable method

T? toNullable()

Unwraps the Result and returns the successful value if present, otherwise returns null without throwing an exception.

Useful for quick evaluations in the presentation layer (UI) or initial state assignments where failures can be safely skipped.

final User? user = userResult.toNullable();

Implementation

T? toNullable() {
  return switch (this) {
    SuccessResult<T>(success: Success(:final value)) => value,
    FailureResult<T>() => null,
  };
}