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
-
- Functor<
_OptionHKT, T> - Applicative<
_OptionHKT, T> - Monad<
_OptionHKT, T> - Extend<
_OptionHKT, T> - Filterable<
_OptionHKT, T>
- Functor<
- Available extensions
Constructors
-
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
aisnull, Some otherwise.factory - Option.fromPredicate(T value, bool predicate(T t))
-
Return Some of
valuewhenpredicateapplied tovaluereturnstrue, 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
fand returnSome(a)when no error are thrown, otherwise returnNone.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
-
alt(
Option< T> orElse()) → Option<T> -
Available on Option<
Return the current Option if it is a Some, otherwise return the result ofT> , provided by the FpdartOnOption extensionorElse. -
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
ato change the value of typeTto 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 another Option.
override
-
elem(
T t, Eq< T> eq) → bool -
Available on Option<
ReturnT> , provided by the FpdartOnOption extensiontruewhen value ofais equal to the value inside the Option. -
extend<
Z> (Z f(Option< T> t)) → Option<Z> -
Change the value of Option from type
Tto typeZbased on the value ofOption<T>using functionf.override -
extract<
T> (K key) → Option< T> -
Available on Option<
Return an Option that conditionally accesses map keys, only if they match the given type.Map< , provided by the FpdartOnOptionMap extensionK, dynamic> > -
extractMap(
K key) → Option< Map< K, dynamic> > -
Available on Option<
Return an Option that conditionally accesses map keys, if they contain a map with the same key type.Map< , provided by the FpdartOnOptionMap extensionK, dynamic> > -
filter(
bool f(T t)) → Option< T> -
If this Option is a Some and calling
freturnstrue, 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
freturns 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
onSomewhen value is Some, otherwise executeonNone. -
getOrElse(
T orElse()) → T -
Available on Option<
If this Option is a Some then return the value inside the Option. Otherwise return the result ofT> , provided by the FpdartOnOption extensionorElse. -
isNone(
) → bool -
Return
truewhen value is None. -
isSome(
) → bool -
Return
truewhen value is Some. -
map<
B> (B f(T t)) → Option< B> -
Change the value of type
Tto a value of typeBusing 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
Tand the value of typeCof 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 typeCof a second Option, and the value of typeDof a third Option.override -
match<
B> (B onNone(), B onSome(T t)) → B -
Execute
onSomewhen value is Some, otherwise executeonNone. -
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
freturns 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
Twhen this Option is a Some,nullotherwise. -
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
eitheris Right, None otherwise. -
fromPredicateMap<
A, B> (A value, bool predicate(A a), B f(A a)) → Option< B> -
Return Some of type
Bby callingfwithvaluewhenpredicateapplied tovalueistrue,Noneotherwise. -
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
emptyvalue is None and thecombinefunction 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
emptyvalue is None and thecombinefunction 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
emptyvalue is None and thecombinefunction uses the givensemigroupto 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< m) → (Option<A, B> >A> , Option<B> ) -
Return a record of Option from a
Option<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>>.