unwrapOrNull method
T?
unwrapOrNull()
Extracts the value if Valid, or returns null if Invalid.
A safe way to access the validated value without distinguishing error states. This method never throws. Use isValid first if you need to know whether validation succeeded, or use asResult to get both the value and error information together.
Example:
final value = validation.unwrapOrNull() ?? defaultValue;
// Safe extraction with null-coalescing
final name = nameValidation.unwrapOrNull();
if (name != null) {
processName(name);
}
Implementation
T? unwrapOrNull() => switch (this) {
Invalid<T>() => null,
Valid<T>(:final value) => value,
};