Optional<T> class

Null-safe value container, similar to Java's Optional.

Encapsulates "possibly absent" values in an explicit wrapper, so you can chain map, filter, ifPresent, etc. instead of scattering if (x == null) checks throughout business logic.

Constructors

Optional.empty()
Empty container.
factory
Optional.of(T value)
Wrap a non-null value. Use ofNullable if value may be null.
factory
Optional.ofNullable(T? value)
Wrap a value that may be null.
factory

Properties

hashCode → int
The hash code for this object.
no setterinherited
isEmpty → bool
Whether the container is empty.
no setter
isPresent → bool
Whether a value is present.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
value → T
The wrapped value; throws StateError when empty.
no setter

Methods

filter(bool predicate(T)) → Optional<T>
Keep the value only if it matches predicate; otherwise become empty.
flatMap<R>(Optional<R> mapper(T)) → Optional<R>
Flat-map into another Optional; stays empty if already empty.
getOrElse(T fallback) → T
Returns the wrapped value, or fallback when empty.
getOrThrow([Object? error]) → T
Returns the wrapped value; throws error (or StateError with error's toString) when empty.
ifPresent(void action(T)) → void
Run action if a value is present.
ifPresentOrElse(void action(T), void orElse()) → void
Run action if present, orElse if empty.
map<R>(R mapper(T)) → Optional<R>
Transform the value; stays empty if already empty.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited