Optional<T> class sealed

A container for a value that may or may not exist. Using a sealed class ensures exhaustive pattern matching.

Implementers

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 value is null.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isEmpty bool
Returns true if this is a None instance.
no setter
isPresent bool
Returns true if this is a Some instance.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

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