Option<T> class sealed

A type that can contain a value of type T in a Some or no value with None.

Used to represent type-safe missing values. Instead of using null, you define the type to be Option. In this way, you are required by the type system to handle the case in which the value is missing.

final Option<String> mStr = Option.of('name');

/// Using [Option] you are required to specify every possible case.
/// The type system helps you to find and define edge-cases and avoid errors.
mStr.match(
  () => print('I have no string to print 🤷‍♀️'),
  printString,
);
Implemented types
Available extensions

Constructors

Option.Do(DoFunctionOption<T> f)
Initialize a Do Notation chain.
factory
Option.flatten(Option<Option<T>> m)
Flat a Option contained inside another Option to be a single Option.
factory
Option.fromJson(dynamic json, T fromJsonT(dynamic json))
Converts from Json.
factory
Option.fromNullable(T? t)
Return None if a is null, Some otherwise.
factory
Option.fromPredicate(T value, bool predicate(T t))
Return Some of value when predicate applied to value returns true, None otherwise.
factory
Option.none()
Return a None.
const
factory
Option.of(T t)
Return a Some(a).
const
factory
Option.safeCast(dynamic value)
Safely cast a value to type T.
factory
Option.tryCatch(T f())
Try to run f and return Some(a) when no error are thrown, otherwise return None.
factory

Properties

asZIO ZIO<NoEnv, None, T>
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
toZIO IOOption<A>

Available on Option<A>, provided by the ZIOOptionExt extension

Alias for ZIO.fromOption
no setter

Methods

alt(Option<T> orElse()) Option<T>

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

Return the current Option if it is a Some, otherwise return the result of orElse.
andThen<B>(covariant Option<B> then()) Option<B>
If this Option is a Some, then return the result of calling then. Otherwise return None.
ap<B>(covariant Option<B Function(T t)> a) Option<B>
Apply the function contained inside a to change the value of type T to a value of type B.
call<B>(covariant Option<B> chain) Option<B>
Chain multiple Options.
duplicate() Option<Option<T>>
Wrap this Option inside another Option.
extend<Z>(Z f(Option<T> t)) Option<Z>
Change the value of Option from type T to type Z based on the value of Option<T> using function f.
extract<T>(K key) Option<T>

Available on Option<Map<K, dynamic>>, provided by the ElementalOnOptionMap extension

Return an Option that conditionally accesses map keys, only if they match the given type.
extractMap(K key) Option<Map<K, dynamic>>

Available on Option<Map<K, dynamic>>, provided by the ElementalOnOptionMap extension

Return an Option that conditionally accesses map keys, if they contain a map with the same key type.
filter(bool f(T t)) Option<T>
If this Option is a Some and calling f returns true, then return this Some. Otherwise return None.
filterMap<Z>(Option<Z> f(T t)) Option<Z>
If this Option is a Some and calling f returns Some, then return this Some. Otherwise return None.
flatMap<B>(covariant Option<B> f(T t)) Option<B>
Used to chain multiple functions that return a Option.
flatMapNullable<B>(B? f(T t)) Option<B>
Return a new Option that calls Option.fromNullable on the result of of the given function f.
flatMapThrowable<B>(B f(T t)) Option<B>
Return a new Option that calls Option.tryCatch with the given function f.
getOrElse(T orElse()) → T

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

If this Option is a Some then return the value inside the Option. Otherwise return the result of orElse.
isNone() bool
Return true when value is None.
isSome() bool
Return true when value is Some.
map<B>(B f(T t)) Option<B>
Change the value of type T to a value of type B using function f.
map2<C, D>(covariant Option<C> mc, D f(T t, C c)) Option<D>
Change type of this Option based on its value of type T and the value of type C of another Option.
map3<C, D, E>(covariant Option<C> mc, covariant Option<D> md, E f(T t, C c, D d)) Option<E>
Change type of this Option based on its value of type T, the value of type C of a second Option, and the value of type D of a third Option.
match<B>(B onNone(), B onSome(T t)) → B
Execute onSome when value is Some, otherwise execute onNone.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
partition(bool f(T t)) → (Option<T>, Option<T>)
Return a record. If this Option is a Some:
partitionMap<Z, Y>(Either<Z, Y> f(T t)) → (Option<Z>, Option<Y>)
Return a record that contains as first value a Some when f returns Left, otherwise the Some will be the second value of the tuple.
toEither<L>(L onLeft()) Either<L, T>
Build an Either from Option.
toJson(Object? toJsonT(T)) Object?
Converts to Json.
toNullable() → T?
Return value of type T when this Option is a Some, null otherwise.
toString() String
A string representation of this object.
inherited
toZIOOrFail<E>(E onNone()) ZIO<NoEnv, E, A>

Available on Option<A>, provided by the ZIOOptionExt extension

Alias for ZIO.fromOptionOrFail

Operators

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

Static Methods

fromEither<L, R>(Either<L, R> either) Option<R>
Build a Option from a Either by returning Some when either is Right, None otherwise.
fromPredicateMap<A, B>(A value, bool predicate(A a), B f(A a)) Option<B>
Return Some of type B by calling f with value when predicate applied to value is true, None otherwise.
safeCastStrict<T, V>(V value) Option<T>
Safely cast a value to type T.
separate<A, B>(Option<Either<A, B>> m) → (Option<A>, Option<B>)
Return a record of Option from a Option<Either<A, B>>.
sequenceList<A>(List<Option<A>> list) Option<List<A>>
Convert a List<Option<A>> to a single Option<List<A>>.
traverseList<A, B>(List<A> list, Option<B> f(A a)) Option<List<B>>
Map each element in the list to an Option using the function f, and collect the result in an Option<List<B>>.
traverseListWithIndex<A, B>(List<A> list, Option<B> f(A a, int i)) Option<List<B>>
Map each element in the list to an Option using the function f, and collect the result in an Option<List<B>>.