Option<T> class
sealed
An optional value.
An Option either contains a value (Some) or it does not (None).
Examples
The following function tries to multiply two integers after parsing them. Upon success, the resulting value is wrapped in a Some. If an error occurs during conversion, a None is returned.
Option<int> multiply(String a, String b) {
return int
.tryParse(a)
.optional
.zip(int.tryParse(b).optional)
.map((ab) => ab.$1 * ab.$2);
}
That function can now be used to try and multiply two integers encoded as strings and returning None if that operation is not possible due to a conversion error.
// prints "Some(6)"
print(multiply('2', '3'));
// prints "None"
print(multiply('two', '3'));
- Implementers
- Available Extensions
- Annotations
-
- @immutable
Constructors
- Option(T? value)
-
Creates a Some with the given
value
, if it is notnull
, or a None otherwise.factory - Option.none()
-
Creates a None.
constfactory
- Option.some(T value)
-
Creates a Some with the given
value
.constfactory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isNone → bool
-
Whether
this
is a None.no setter - isSome → bool
-
Whether
this
is a Some.no setter -
iterable
→ Iterable<
T> -
Returns an iterable over the possibly contained value.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- valueOrNull → T?
-
The contained value if
this
is a Some, ornull
otherwise.no setter
Methods
-
and<
U> (Option< U> other) → Option<U> -
Returns
other
ifthis
is a Some, or None otherwise. -
andThen<
U> (Option< U> calculateOther(T value)) → Option<U> -
Returns the result of
calculateOther
ifthis
is a Some, or None otherwise. -
contains(
T value) → bool -
Returns
true
ifthis
is a Some of the givenvalue
. -
inspect(
void inspect(T value)) → Option< T> -
Calls inspect with the contained value if
this
is a Some. -
isSomeAnd(
bool condition(T value)) → bool -
Returns
true
ifthis
is a Some with a contained value that satisfiescondition
. -
map<
U> (U map(T value)) → Option< U> - Transforms the contained value, if any, by applying map to it.
-
mapOr<
U> (U map(T value), U defaultValue) → U -
Returns the contained value, if any, with
map
applied to it, ordefaultValue
otherwise. -
mapOrElse<
U> (U map(T value), U calculateDefaultValue()) → U -
Returns the contained value, if any, with
map
applied to it, or the result ofcalculateDefaultValue
otherwise. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
okOr<
E> (E error) → Result< T, E> -
Transforms an Option into a Result, mapping Some to an Ok of the contained value and
None to Err of
error
. -
okOrElse<
E> (E calculateError()) → Result< T, E> -
Transforms an Option into a Result, mapping Some to an Ok of the contained value and
None to Err of the result of
calculateError
. -
or(
Option< T> other) → Option<T> -
Returns a Some of the original value if
this
is a Some, orother
otherwise. -
orElse(
Option< T> calculateOther()) → Option<T> -
Returns a Some of the original value if
this
is a Some, or the result ofcalculateOther
otherwise. -
toString(
) → String -
A string representation of this object.
inherited
-
unwrap(
{String? msg}) → T - Returns the contained value.
-
unwrapOr(
T defaultValue) → T -
Returns the contained value, if any, or
defaultValue
otherwise. -
unwrapOrElse(
T calculateDefaultValue()) → T -
Returns the contained value, if any, or the result of
calculateDefaultValue
otherwise. -
where(
bool condition(T value)) → Option< T> -
Returns a Some of the original value if
this
is a Some and its contained value satisfiescondition
, or None otherwise. -
whereType<
U> () → Option< U> -
Returns a Some of the original value if
this
is a Some with a contained value of typeU
, or None otherwise. -
xor(
Option< T> other) → Option<T> -
Returns a Some if either, but not both, of
this
andother
is a Some, or None otherwise. -
zip<
U> (Option< U> other) → Option<(T, U)> -
Returns a tuple of both
this
andother
if both are Some, or None otherwise. -
zipWith<
U, R> (Option< U> other, R zip(T value, U otherValue)) → Option<R> -
Returns the result of
zip
called with boththis
andother
if both are Some, or None otherwise.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
collect<
T> (Option< T> collector(U check<U>(Option< )) → Option<U> option)T> - Encloses any number of operations, optionally returning early on None.
-
collectAsync<
T> (FutureOr< Option< collector(U check<T> >U>(Option< )) → Future<U> option)Option< T> > - Encloses any number of operations, optionally returning early on None.