Option<T> class
sealed
A type that represents the presence (Some) or absence (None) of a value.
Pattern matching is recommended for working with Option types.
Option<int> foo = Some(42);
print(switch (foo) {
Some(value: var bar) => 'Some value: $bar',
None() => 'No value!'
});
See also:
Rust: Option
- Implementers
- Available extensions
Constructors
- Option.from(T? value)
-
Creates an
Optionfrom the given nullableTvalue.factory
Properties
- hashCode → int
-
The hash code for this object.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
and<
U> (Option< U> other) → Option<U> -
Returns None<U> if this
Optionis None<T>, otherwise returnsother. -
andThen<
U> (Option< U> fn(T)) → Option<U> -
Returns None<U> if this
Optionis None<T>, otherwise callsfnwith the held value and returns the returnedOption. -
call(
) → T - Shortcut to call Option.unwrap().
-
expect(
String message) → T -
Returns the held value of this
Optionif it is Some, or throws OptionError with the givenmessageif thisOptionis None. -
flatten(
) → Option< T> -
Available on Option<
Flattens a nestedOption< , provided by the OptionFlatten extensionT> >Optiontype value one level. -
inspect(
void fn(T)) → Option< T> -
Calls the provided function with the contained value if this
Optionis Some. -
isNone(
) → bool -
Returns whether or not this
Optionholds no value (None). -
isSome(
) → bool -
Returns whether or not this
Optionholds a value (Some). -
isSomeAnd(
bool predicate(T)) → bool -
Returns whether or not this
Optionholds a value (Some) and the held value matches the given predicate. -
iter(
) → Iterable< T> - Returns an Iterable of the held value.
-
map<
U> (U mapFn(T)) → Option< U> -
Maps this
Option<T>to anOption<U>using the given function with the held value. -
mapOr<
U> (U orValue, U mapFn(T)) → Option< U> -
Maps this
Option<T>to anOption<U>using the given function with the held value if thisOption<T>is Some. Otherwise returns the providedorValueasSome(orValue). -
mapOrElse<
U> (U orFn(), U mapFn(T)) → Option< U> -
Maps this
Option<T>to anOption<U>using the givenmapFnfunction with the held value if thisOptionis Some. Otherwise returns the result oforFnasSome(orFn()). -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
okOr<
E> (E err) → Result< T, E> -
Converts this
Option<T>into a Result<T, E> using the givenerrif None. -
okOrElse<
E> (E elseFn()) → Result< T, E> -
Converts this
Option<T>into a Result<T, E> using the returned value fromelseFnif None. -
or(
Option< T> other) → Option<T> -
Returns this
Optionif thisOptionis Some<T>, otherwise returnsother. -
orElse(
Option< T> fn()) → Option<T> -
Returns this
Optionif thisOptionis Some<T>, otherwise callsfnand returns the returnedOption. -
toString(
) → String -
A string representation of this object.
override
-
transpose(
) → Result< Option< T> , E> -
Available on Option<
Transposes this Option<Result<T, E>> into a Result<Option<T>, E>.Result< , provided by the OptionTranspose extensionT, E> > -
unwrap(
) → T -
Returns the held value of this
Optionif it is Some. -
unwrapOr(
T orValue) → T -
Returns the held value of this
Optionif it is Some, or the given value if thisOptionis None. -
unwrapOrElse(
T elseFn()) → T -
Returns the held value of this
Optionif it is Some, or returns the returned value fromelseFnif thisOptionis None. -
unzip(
) → (Option< T> , Option<U> ) -
Available on Option<
Unzips this(T, U)> , provided by the OptionUnzip extensionOptionif thisOptionholds a Record of two values. -
where(
bool predicate(T)) → Option< T> -
Filters this
Optionbased on the givenpredicatefunction. -
xor(
Option< T> other) → Option<T> -
Returns Some if exactly one of this
Optionandotheris Some, otherwise returns None. -
zip<
U> (Option< U> other) → Option<(T, U)> -
Zips this
Optionwith anotherOption, returning a Record of their held values. -
zipWith<
U, V> (Option< U> other, V zipFn(T, U)) → Option<V> -
Zips this
Optionwith anotherOptionusing the given function.
Operators
-
operator ==(
Object other) → bool -
Compare equality between two
Optionvalues.override -
operator ~(
) → T -
Deprecated: Use
Option.call()asvalue()to easily unwrapOptiontype values instead.