Option<T> class sealed

Optional value wrapper for Rust-style present-or-absent values.

Use None() when no value is present, and Some(value) when a value is present even if that value is null.

const nickname = Some<String?>(null);

final label = switch (nickname) {
  None<String?>() => 'missing',
  Some<String?>(:final value) => value ?? 'present null',
};
Available extensions

Constructors

Option.fromNullable(T? value)
Wraps a nullable value: null becomes None, anything else Some.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isNone bool
Whether this option is None.
no setter
isSome bool
Whether this option is Some.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

and<R>(Option<R> other) Option<R>

Available on Option<T>, provided by the OptionTransform extension

Returns other when this option is present, and None otherwise.
andThen<R>(Option<R> next(T value)) Option<R>
Chains another option-producing operation when a value is present.
contains(T value) bool

Available on Option<T>, provided by the OptionQuery extension

Whether this option holds a value equal to value.
expect(String message) → T

Available on Option<T>, provided by the OptionQuery extension

Returns the present value, or throws message when absent.
filter(bool predicate(T value)) Option<T>

Available on Option<T>, provided by the OptionTransform extension

Keeps a present value only when it satisfies predicate.
flatten() Option<T>

Available on Option<Option<T>>, provided by the OptionFlatten extension

Turns Some(Some(value)) into Some(value), and any absence into None.
inspect(void action(T value)) Option<T>

Available on Option<T>, provided by the OptionTransform extension

Calls action with a present value, and returns this option unchanged.
isNoneOr(bool predicate(T value)) bool

Available on Option<T>, provided by the OptionQuery extension

Whether the option is absent, or its value satisfies predicate.
isSomeAnd(bool predicate(T value)) bool

Available on Option<T>, provided by the OptionQuery extension

Whether a value is present and satisfies predicate.
map<R>(R mapper(T value)) Option<R>
Maps a present value and leaves absent values unchanged.
mapOr<R>(R fallback, R mapper(T value)) → R

Available on Option<T>, provided by the OptionTransform extension

Maps a present value, or returns fallback when absent.
mapOrElse<R>(R fallback(), R mapper(T value)) → R

Available on Option<T>, provided by the OptionTransform extension

Maps a present value, or computes a fallback when absent.
match<R>({required R some(T value), required R none()}) → R
Pattern matches this option.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
okOr<E>(E error) Result<T, E>

Available on Option<T>, provided by the OptionCombine extension

Returns Ok with a present value, or Err with error when absent.
okOrElse<E>(E error()) Result<T, E>

Available on Option<T>, provided by the OptionCombine extension

Returns Ok with a present value, or computes an Err when absent.
or(Option<T> other) Option<T>

Available on Option<T>, provided by the OptionTransform extension

Returns this option when present, and other otherwise.
orElse(Option<T> fallback()) Option<T>

Available on Option<T>, provided by the OptionTransform extension

Returns this option when present, and computes one otherwise.
toIterable() Iterable<T>

Available on Option<T>, provided by the OptionQuery extension

Returns the present value as a one-element iterable, or an empty one.
toNullable() → T?

Available on Option<T>, provided by the OptionQuery extension

Returns the present value, or null when absent.
toString() String
A string representation of this object.
inherited
transpose() Result<Option<T>, E>

Available on Option<Result<T, E>>, provided by the OptionTranspose extension

Turns an optional result into a result of an option.
unwrap() → T

Available on Option<T>, provided by the OptionQuery extension

Returns the present value, or throws when absent.
unwrapOr(T fallback) → T
Returns the present value, or fallback when this option is absent.
unwrapOrElse(T fallback()) → T
Returns the present value, or computes one when this option is absent.
unzip() → (Option<A>, Option<B>)

Available on Option<(A, B)>, provided by the OptionUnzip extension

Splits a present pair into two present options, and absence into two absent ones.
xor(Option<T> other) Option<T>

Available on Option<T>, provided by the OptionTransform extension

Returns whichever of the two options is present, when exactly one is.
zip<U>(Option<U> other) Option<(T, U)>

Available on Option<T>, provided by the OptionCombine extension

Pairs this value with other's, when both are present.
zipWith<U, R>(Option<U> other, R combine(T left, U right)) Option<R>

Available on Option<T>, provided by the OptionCombine extension

Combines this value with other's, when both are present.

Operators

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