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:
nullbecomes None, anything else Some.factory
Properties
Methods
-
and<
R> (Option< R> other) → Option<R> -
Available on Option<
ReturnsT> , provided by the OptionTransform extensionotherwhen 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<
Whether this option holds a value equal toT> , provided by the OptionQuery extensionvalue. -
expect(
String message) → T -
Available on Option<
Returns the present value, or throwsT> , provided by the OptionQuery extensionmessagewhen absent. -
filter(
bool predicate(T value)) → Option< T> -
Available on Option<
Keeps a present value only when it satisfiesT> , provided by the OptionTransform extensionpredicate. -
flatten(
) → Option< T> -
Available on Option<
TurnsOption< , provided by the OptionFlatten extensionT> >Some(Some(value))intoSome(value), and any absence into None. -
inspect(
void action(T value)) → Option< T> -
Available on Option<
CallsT> , provided by the OptionTransform extensionactionwith a present value, and returns this option unchanged. -
isNoneOr(
bool predicate(T value)) → bool -
Available on Option<
Whether the option is absent, or its value satisfiesT> , provided by the OptionQuery extensionpredicate. -
isSomeAnd(
bool predicate(T value)) → bool -
Available on Option<
Whether a value is present and satisfiesT> , provided by the OptionQuery extensionpredicate. -
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<
Maps a present value, or returnsT> , provided by the OptionTransform extensionfallbackwhen absent. -
mapOrElse<
R> (R fallback(), R mapper(T value)) → R -
Available on Option<
Maps a present value, or computes a fallback when absent.T> , provided by the OptionTransform extension -
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<
Returns Ok with a present value, or Err withT> , provided by the OptionCombine extensionerrorwhen absent. -
okOrElse<
E> (E error()) → Result< T, E> -
Available on Option<
Returns Ok with a present value, or computes an Err when absent.T> , provided by the OptionCombine extension -
or(
Option< T> other) → Option<T> -
Available on Option<
Returns this option when present, andT> , provided by the OptionTransform extensionotherotherwise. -
orElse(
Option< T> fallback()) → Option<T> -
Available on Option<
Returns this option when present, and computes one otherwise.T> , provided by the OptionTransform extension -
toIterable(
) → Iterable< T> -
Available on Option<
Returns the present value as a one-element iterable, or an empty one.T> , provided by the OptionQuery extension -
toNullable(
) → T? -
Available on Option<
Returns the present value, orT> , provided by the OptionQuery extensionnullwhen absent. -
toString(
) → String -
A string representation of this object.
inherited
-
transpose(
) → Result< Option< T> , E> -
Available on Option<
Turns an optional result into a result of an option.Result< , provided by the OptionTranspose extensionT, E> > -
unwrap(
) → T -
Available on Option<
Returns the present value, or throws when absent.T> , provided by the OptionQuery extension -
unwrapOr(
T fallback) → T -
Returns the present value, or
fallbackwhen 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<
Splits a present pair into two present options, and absence into two absent ones.(A, B)> , provided by the OptionUnzip extension -
xor(
Option< T> other) → Option<T> -
Available on Option<
Returns whichever of the two options is present, when exactly one is.T> , provided by the OptionTransform extension -
zip<
U> (Option< U> other) → Option<(T, U)> -
Available on Option<
Pairs this value withT> , provided by the OptionCombine extensionother's, when both are present. -
zipWith<
U, R> (Option< U> other, R combine(T left, U right)) → Option<R> -
Available on Option<
Combines this value withT> , provided by the OptionCombine extensionother's, when both are present.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited