Either< L, R> class
sealed
Represents a value of one of two possible types, Left or Right.
Either is commonly used to handle errors. Instead of returning placeholder
values when a computation may fail (such as -1
, null
, etc.), we return an instance
of Right containing the correct result when a computation is successful, otherwise we return
an instance of Left containing information about the kind of error that occurred.
Constructors
- Either()
-
const
-
Either.Do(DoFunctionEither<
L, R> f) -
Initialize a Do Notation chain.
factory
-
Either.flatten(Either<
L, Either< L, R>> e) -
Flat a Either contained inside another Either to be a single Either.
factory
- Either.fromNullable(R? r, L onNull())
-
If
r
isnull
, then return the result ofonNull
in Left. Otherwise returnRight(r)
.factory -
Either.fromOption(Option<
R> m, L onNone()) -
Return an Either from a Option:
factory
- Either.fromPredicate(R r, bool predicate(R r), L onFalse(R r))
-
If calling
predicate
withr
returnstrue
, then returnRight(r)
. Otherwise return Left containing the result ofonFalse
.factory - Either.left(L l)
-
Return a
Left(l)
.factory - Either.of(R r)
-
Return a
Right(r)
.factory - Either.right(R r)
-
Return a
Right(r)
.factory - Either.safeCast(dynamic value, L onError(dynamic value))
-
Safely cast a value to type
R
.factory - Either.tryCatch(R run(), L onError(Object o, StackTrace s))
-
Try to execute
run
. If no error occurs, then return Right. Otherwise return Left containing the result ofonError
.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
-
all(
bool predicate(R a)) → bool -
Return the result of
predicate
applied to the value of Right. If the Either is Left, returnstrue
.override -
alt(
covariant Either< L, R> orElse()) → Either< L, R> -
Return the current Either if it is a Right, otherwise return the result of
orElse
.override -
andThen<
R2>( covariant Either< L, R2> then()) → Either< L, R2> -
If this Either is a Right, then return the result of calling
then
. Otherwise return Left.override -
any(
bool predicate(R a)) → bool -
Return the result of
predicate
applied to the value of Right. If the Either is Left, returnsfalse
.override -
ap<
C>( covariant Either< L, C Function(R r)> a) → Either< L, C> -
Apply the function contained inside
a
to change the value on the Right from typeR
to a value of typeC
.override -
bimap<
C, D>( C mLeft(L l), D mRight(R b)) → Either< C, D> - Define two functions to change both the Left and Right value of the Either.
-
bind<
R2>( Either< L, R2> f(R r)) → Either< L, R2> - Used to chain multiple functions that return a Either.
-
bindFuture<
R2>( Future< Either< L, R2>> f(R r)) → TaskEither< L, R2> -
Used to chain multiple functions that return a
Future<Either>
. -
call<
B>( covariant Either< L, B> chain) → Either< L, B> -
Chain multiple functions having the same left type
L
.override -
chainFirst<
C>( covariant Either< L, C> chain(R b)) → Either< L, R> -
Chain a request that returns another Either, execute it, ignore
the result, and return the same value as the current Either.
override
-
concatenate(
Monoid< R> monoid) → R -
Use
monoid
to combine the value of Right.override -
duplicate(
) → Either< L, Either< L, R>> -
Wrap this
Either
inside anotherEither
.override -
elem(
R r, Eq< R> eq) → bool -
Return
true
when value ofr
is equal to the value inside this Either. If this Either is Left, then returnfalse
. -
exists(
bool predicate(R r)) → bool -
Return the result of calliing
predicate
with the value of Either if it is a Right. Otherwise returnfalse
. -
extend<
Z>( Z f(Either< L, R> t)) → Either< L, Z> -
Change the value of Either from type
R
to typeZ
based on the value ofEither<L, R>
using functionf
.override -
filterOrElse(
bool f(R r), L onFalse(R r)) → Either< L, R> -
If
f
applied on this Either as Right returnstrue
, then return this Either. If it returnsfalse
, return the result ofonFalse
in a Left. -
flatMap<
C>( covariant Either< L, C> f(R a)) → Either< L, C> -
Used to chain multiple functions that return a Either.
override
-
fold<
C>( C onLeft(L l), C onRight(R r)) → C -
Execute
onLeft
when value is Left, otherwise executeonRight
. -
foldLeft<
C>( C b, C f(C acc, R b)) → C -
Return the result of
f
called withb
and the value of Right. If this Either is Left, returnb
.override -
foldLeftWithIndex<
C>( C c, C f(int i, C acc, R b)) → C -
Return the result of
f
called withb
and the value of Right. If this Either is Left, returnb
.override -
foldMap<
C>( Monoid< C> monoid, C f(R b)) → C -
Use
monoid
to combine the value of Right applied tof
.override -
foldRight<
C>( C b, C f(C acc, R b)) → C -
Return the result of
f
called withb
and the value of Right. If this Either is Left, returnb
.override -
foldRightWithIndex<
C>( C c, C f(int i, C acc, R b)) → C -
Return the result of
f
called withb
and the value of Right. If this Either is Left, returnb
.override -
getLeft(
) → Option< L> - Extract the value from Left in a Option.
-
getOrElse(
R orElse(L l)) → R -
Return the value inside this Either if it is a Right.
Otherwise return result of
onElse
. -
getRight(
) → Option< R> - Extract the value from Right in a Option.
-
isLeft(
) → bool -
Return
true
when this Either is Left. -
isRight(
) → bool -
Return
true
when this Either is Right. -
length(
) → int -
Returns
1
when Either is Right,0
otherwise.override -
map<
C>( C f(R a)) → Either< L, C> -
If the Either is Right, then change its value from type
R
to typeC
using functionf
.override -
map2<
C, D>( covariant Either< L, C> m1, D f(R b, C c)) → Either< L, D> -
Change type of this Either based on its value of type
R
and the value of typeC
of another Either.override -
map3<
C, D, E>( covariant Either< L, C> m1, covariant Either< L, D> m2, E f(R b, C c, D d)) → Either< L, E> -
Change type of this Either based on its value of type
R
, the value of typeC
of a second Either, and the value of typeD
of a third Either.override -
mapLeft<
C>( C f(L a)) → Either< C, R> -
If the Either is Left, then change its value from type
L
to typeC
using functionf
. -
match<
C>( C onLeft(L l), C onRight(R r)) → C -
Execute
onLeft
when value is Left, otherwise executeonRight
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
orElse<
L1>( Either< L1, R> onLeft(L l)) → Either< L1, R> -
If this Either is Left, return the result of
onLeft
. -
pure<
C>( C c) → Either< L, C> -
Return a Right containing the value
c
.override -
swap(
) → Either< R, L> - Swap the values contained inside the Left and Right of this Either.
-
toIOEither(
) → IOEither< L, R> - Convert this Either to a IOEither.
-
toNullable(
) → R? -
Convert Either to nullable
R?
. -
toOption(
) → Option< R> - Convert this Either to a Option:
-
toString(
) → String -
A string representation of this object.
inherited
-
toTaskEither(
) → TaskEither< L, R> - Convert this Either to a TaskEither.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
getEq<
L, R>( Eq< L> eqL, Eq< R> eqR) → Eq< Either< L, R>> -
Build an
Eq<Either>
by comparing the values inside twoEither
. -
getSemigroup<
L, R>( Semigroup< R> semigroup) → Semigroup< Either< L, R>> -
Build a
Semigroup<Either>
from a Semigroup. -
lefts<
E, A>( List< Either< E, A>> list) → List< E> -
Extract all the Left values from a
List<Either<E, A>>
. -
partitionEithers<
E, A>( List< Either< E, A>> list) → (List< E>, List< A>) -
Extract all the Left and Right values from a
List<Either<E, A>>
and return them in two partitioned List inside a record. -
rights<
E, A>( List< Either< E, A>> list) → List< A> -
Extract all the Right values from a
List<Either<E, A>>
. -
safeCastStrict<
L, R, V>( V value, L onError(V value)) → Either< L, R> -
Safely cast a value to type
R
. -
sequenceList<
E, A>( List< Either< E, A>> list) → Either< E, List< A>> -
Convert a
List<Either<E, A>>
to a singleEither<E, List<A>>
. -
traverseList<
E, A, B>( List< A> list, Either< E, B> f(A a)) → Either< E, List< B>> -
Map each element in the list to an Either using the function
f
, and collect the result in anEither<E, List<B>>
. -
traverseListWithIndex<
E, A, B>( List< A> list, Either< E, B> f(A a, int i)) → Either< E, List< B>> -
Map each element in the list to an Either using the function
f
, and collect the result in anEither<E, List<B>>
. -
tryCatchK<
L, R, T>( R run(T), L onError(Object o, StackTrace s)) → Either< L, R> Function(T) -
Try to execute
run
. If no error occurs, then return Right. Otherwise return Left containing the result ofonError
.