Option<T> class
abstract
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
-
- Functor<
_OptionHKT, T> - Applicative<
_OptionHKT, T> - Monad<
_OptionHKT, T> - Extend<
_OptionHKT, T> - Filterable<
_OptionHKT, T>
- Functor<
- Implementers
- Available Extensions
Constructors
- Option()
-
const
-
Option.Do(DoFunctionOption<
T> f) -
Initialize a Do Notation chain.
factory
-
Option.flatten(Option<
Option< m)T> > -
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
isnull
, Some otherwise.factory - Option.fromPredicate(T value, bool predicate(T t))
-
Return Some of
value
whenpredicate
applied tovalue
returnstrue
, None otherwise.factory - Option.none()
-
Return a None.
constfactory
- Option.of(T t)
-
Return a
Some(a)
.constfactory - Option.safeCast(dynamic value)
-
Safely cast a value to type
T
.factory - Option.tryCatch(T f())
-
Try to run
f
and returnSome(a)
when no error are thrown, otherwise returnNone
.factory
Properties
- hashCode → int
-
The hash code for this object.
read-onlyinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-onlyinherited
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 typeT
to a value of typeB
.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 anotherOption
.override -
extend<
Z> (Z f(Option< T> t)) → Option<Z> -
Change the value of Option from type
T
to typeZ
based on the value ofOption<T>
using functionf
.override -
filter(
bool f(T t)) → Option< T> -
If this Option is a Some and calling
f
returnstrue
, 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 executeonNone
. -
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 typeB
using functionf
.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 typeC
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 typeC
of a second Option, and the value of typeD
of a third Option.override -
match<
B> (B onNone(), B onSome(T t)) → B -
Execute
onSome
when value is Some, otherwise executeonNone
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
partition(
bool f(T t)) → Tuple2< Option< T> , Option<T> > -
Return a Tuple2. If this
Option
is a Some:override -
partitionMap<
Z, Y> (Either< Z, Y> f(T t)) → Tuple2<Option< Z> , Option<Y> > -
Return a Tuple2 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 callingf
withvalue
whenpredicate
applied tovalue
istrue
,None
otherwise. -
getEq<
T> (Eq< T> eq) → Eq<Option< T> > -
Build an
Eq<Option>
by comparing the values inside twoOption
. -
getFirstMonoid<
T> () → Monoid< Option< T> > -
Build an instance of Monoid in which the
empty
value is None and thecombine
function is based on the firstOption
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 thecombine
function is based on the secondOption
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 thecombine
function uses the givensemigroup
to combine the values of bothOption
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< m) → Tuple2<A, B> >Option< A> , Option<B> > -
Return a Tuple2 of
Option
from aOption<Either<A, B>>
. -
sequenceList<
A> (List< Option< list) → Option<A> >List< A> > -
Convert a
List<Option<A>>
to a singleOption<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 anOption<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 anOption<List<B>>
.