Optional<T> class
sealed
A container for a value that may or may not exist. Using a sealed class ensures exhaustive pattern matching.
Constructors
- Optional.none()
-
Creates an empty Optional.
factory
- Optional.of(T value)
-
Creates an Optional containing
value.factory - Optional.ofNullable(T? value)
-
Wraps a nullable value. Returns None if
valueis null.factory
Properties
Methods
-
flatMap<
R> (Optional< R> mapper(T value)) → Optional<R> -
Transforms the value using a function that returns an Optional.
Prevents nested structures like
Optional<Optional<T>>. -
map<
R> (R mapper(T value)) → Optional< R> -
Transforms the value if present using
mapper. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
valueOr(
T fallback) → T -
Returns the value if present, otherwise returns
fallback.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited