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,
);
Inheritance
Mixed in types
Implementers
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

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

andThen<B>(covariant Option<B> then()) Option<B>
If this Option is a Some, then return the result of calling then. Otherwise return None.
override
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.
override
call<B>(covariant Option<B> chain) Option<B>
Chain multiple Options.
override
chainFirst<B>(covariant Monad<_OptionHKT, B> chain(T a)) HKT<_OptionHKT, T>
inherited
duplicate() Option<Option<T>>
Wrap this Option inside another Option.
override
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.
override
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.
override
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.
override
flatMap<B>(covariant Option<B> f(T t)) Option<B>
Used to chain multiple functions that return a Option.
override
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.
fold<B>(B onNone(), B onSome(T t)) → B
Execute onSome when value is Some, otherwise execute onNone.
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.
override
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.
override
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.
override
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:
override
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.
override
pure<B>(B b) Option<B>
Return a Some containing the value b.
override
toEither<L>(L onLeft()) Either<L, T>
Build an Either from Option.
toIOOption() IOOption<T>
Convert this Option to a IOOption.
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
toTaskOption() TaskOption<T>
Convert this Option to a TaskOption.

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.
getEq<T>(Eq<T> eq) Eq<Option<T>>
Build an Eq<Option> by comparing the values inside two Option.
getFirstMonoid<T>() Monoid<Option<T>>
Build an instance of Monoid in which the empty value is None and the combine function is based on the first Option if it is Some, otherwise the second.
getLastMonoid<T>() Monoid<Option<T>>
Build an instance of Monoid in which the empty value is None and the combine function is based on the second Option if it is Some, otherwise the first.
getMonoid<T>(Semigroup<T> semigroup) Monoid<Option<T>>
Build an instance of Monoid in which the empty value is None and the combine function uses the given semigroup to combine the values of both Option if they are both Some.
getOrder<T>(Order<T> order) Order<Option<T>>
Return an Order to order instances of Option.
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>>.