Optional<T> class sealed

A container object which may or may not contain a value of type T.

Note that depending on the nullability of type T, a null value can be a perfectly valid present value.

Also see https://en.wikipedia.org/wiki/Option_type.

Annotations
  • @immutable

Constructors

Optional.absent()
Returns an absent Optional instance.
const
factory
Optional.of(T value)
Returns a present Optional of the given value.
const
factory
Optional.ofNullable(T? value)
Returns a present Optional of the given value if the value is non-null, otherwise an absent Optional instance.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isAbsent bool
Returns whether the value is absent.
no setter
isPresent bool
Returns whether the value is present.
no setter
iterable Iterable<T>
Returns an Iterable with the single present value, otherwise an empty iterable.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value → T
Returns the present value, otherwise throws a StateError.
no setter

Methods

flatMap<R>(Map1<T, Optional<R>> function) Optional<R>
Transforms the present value of this Optional with the provided mapping function, otherwise returns an absent Optional.
ifAbsent(Callback0 function) → void
Evaluates the callback function if the value is absent, otherwise does nothing.
ifPresent(Callback1<T> function) → void
Evaluates the callback function with the present value, otherwise does nothing.
map<R>(Map1<T, R> function) Optional<R>
Transforms the present value of this Optional with the provided mapping function, otherwise returns an absent Optional.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
or(Map0<Optional<T>> function) Optional<T>
Returns the current optional if present, otherwise evaluates the function callback and returns its optional.
orElse(T value) → T
Returns the current value if present, otherwise the argument value.
orElseGet(Map0<T> function) → T
Returns the current value if present, otherwise evaluates the callback function and returns its value.
orElseThrow([Object? error]) → T
Returns the current value if present, otherwise throws the provided error or a StateError.
toString() String
A string representation of this object.
inherited
where(Predicate1<T> function) Optional<T>
Returns this Optional if the provided predicate function evaluates to true, otherwise returns an absent Optional.
whereType<R>() Optional<R>
Casts the present value of this Optional to R, otherwise returns an absent Optional.

Operators

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