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 not null, or a None otherwise.
factory
Option.none()
Creates a None.
const
factory
Option.some(T value)
Creates a Some with the given value.
const
factory

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, or null otherwise.
no setter

Methods

and<U>(Option<U> other) Option<U>
Returns other if this is a Some, or None otherwise.
andThen<U>(Option<U> calculateOther(T value)) Option<U>
Returns the result of calculateOther if this is a Some, or None otherwise.
contains(T value) bool
Returns true if this is a Some of the given value.
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 if this is a Some with a contained value that satisfies condition.
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, or defaultValue otherwise.
mapOrElse<U>(U map(T value), U calculateDefaultValue()) → U
Returns the contained value, if any, with map applied to it, or the result of calculateDefaultValue 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, or other otherwise.
orElse(Option<T> calculateOther()) Option<T>
Returns a Some of the original value if this is a Some, or the result of calculateOther 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 satisfies condition, or None otherwise.
whereType<U>() Option<U>
Returns a Some of the original value if this is a Some with a contained value of type U, or None otherwise.
xor(Option<T> other) Option<T>
Returns a Some if either, but not both, of this and other is a Some, or None otherwise.
zip<U>(Option<U> other) Option<(T, U)>
Returns a tuple of both this and other 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 both this and other 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<U> option))) Option<T>
Encloses any number of operations, optionally returning early on None.
collectAsync<T>(FutureOr<Option<T>> collector(U check<U>(Option<U> option))) Future<Option<T>>
Encloses any number of operations, optionally returning early on None.