Either<L, R> class
abstract
A generic type that represents a value of one of two possible types (a disjoint union).
Instances of Either are either an instance of Left or Right.
- Left is used to represent failure, typically holding an error or exception.
- Right is used to represent success, typically holding a valid result.
Either<L, R> is commonly used as a functional alternative to throwing exceptions.
Example:
void main() {
final result1 = divide(10, 2);
final result2 = divide(5, 0);
result1.fold(
(error) => print('Error: $error'),
(value) => print('Result: $value'),
); // Prints: Result: 5
result2.fold(
(error) => print('Error: $error'),
(value) => print('Result: $value'),
); // Prints: Error: Cannot divide by zero
// Using map
final mappedResult = result1.map((value) => value * 2);
print(mappedResult.getRight()); // Prints: 10
}
Either<String, int> divide(int a, int b) {
if (b == 0) {
return const Left('Cannot divide by zero');
} else {
return Right(a ~/ b);
}
}
Properties
- errorOrNull → E?
-
Available on Either<
The validation error, orE, T> , provided by the GmanaValueObjectResultX extensionnullwhen validation succeeded.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isInvalid → bool
-
Available on ValidationResult<
Whether validation failed.TIssue, TValue> , provided by the GmanaValidationResultX extensionno setter - isInvalid → bool
-
Available on Either<
Whether validation failed.E, T> , provided by the GmanaValueObjectResultX extensionno setter - issueOrNull → TIssue?
-
Available on ValidationResult<
The typed validation issue, orTIssue, TValue> , provided by the GmanaValidationResultX extensionnullwhen validation succeeded.no setter - isValid → bool
-
Available on ValidationResult<
Whether validation succeeded.TIssue, TValue> , provided by the GmanaValidationResultX extensionno setter - isValid → bool
-
Available on Either<
Whether validation succeeded.E, T> , provided by the GmanaValueObjectResultX extensionno setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- valueOrNull → TValue?
-
Available on ValidationResult<
The validated value, orTIssue, TValue> , provided by the GmanaValidationResultX extensionnullwhen validation failed.no setter - valueOrNull → T?
-
Available on Either<
The validated value, orE, T> , provided by the GmanaValueObjectResultX extensionnullwhen validation failed.no setter
Methods
-
all(
bool test(R right)) → bool -
Returns
truewhen this is a Left or whentestpasses for the Right value. -
bimap<
L2, R2> (L2 ifLeft(L left), R2 ifRight(R right)) → Either< L2, R2> -
Applies
ifLeftorifRightand returns a new Either with mapped values. -
contains(
R value) → bool -
Returns
truewhen this is a Right containingvalue. -
exists(
bool test(R right)) → bool -
Returns
truewhen this is a Right andtestpasses. -
filterOrElse(
bool test(R right), L onFalse(R right)) → Either< L, R> -
Demotes a Right to a Left when
testfails. -
flatMap<
R2> (Either< L, R2> f(R right)) → Either<L, R2> -
Applies the function
fto the value contained in Right, if it exists, and returns a new Either containing the result. If this is a Left, it is returned unchanged. -
flatMapAsync<
R2> (FutureOr< Either< f(R right)) → Future<L, R2> >Either< L, R2> > -
Asynchronously applies
fto the Right value, if present. -
flatMapLeft<
L2> (Either< L2, R> f(L left)) → Either<L2, R> - Chains on the Left side — the mirror of flatMap.
-
fold<
B> (B ifLeft(L left), B ifRight(R right)) → B - Applies one of two functions depending on whether this is a Left or Right.
-
foldAsync<
B> (FutureOr< B> ifLeft(L left), FutureOr<B> ifRight(R right)) → Future<B> - Asynchronously applies one of two functions depending on the side present.
-
getLeft(
) → L - Returns the Left value if this is a Left, otherwise throws.
-
getOrDefault(
R defaultValue) → R -
Returns the Right value, or
defaultValuewhen this is a Left. -
getOrElse(
R orElse(L left)) → R - Returns the Right value if this is a Right, otherwise computes a fallback.
-
getOrNull(
) → R? -
Returns the Right value if this is a Right, otherwise
null. -
getRight(
) → R - Returns the Right value if this is a Right, otherwise throws.
-
isLeft(
) → bool -
Returns
trueif this is a Left. -
isRight(
) → bool -
Returns
trueif this is a Right. -
leftOrNull(
) → L? -
Returns the Left value if this is a Left, otherwise
null. -
map<
R2> (R2 f(R right)) → Either< L, R2> -
Transforms the value contained in Right using the given function
f, returning a new Either with the transformed value. -
mapAsync<
R2> (FutureOr< R2> f(R right)) → Future<Either< L, R2> > -
Asynchronously transforms the Right value using
f, if present. -
mapLeft<
L2> (L2 f(L left)) → Either< L2, R> -
Maps the Left value using
f, if present. -
messageOrNull(
[ValidationErrorMessages messages = const DefaultValidationErrorMessages()]) → String? -
Available on Either<
Resolves the validation error to a message, or returnsE, T> , provided by the GmanaValueObjectResultX extensionnullon success. -
messageOrNull(
ValidationMessageResolver< TIssue> resolve) → String? -
Available on ValidationResult<
Resolves the validation issue to a message, or returnsTIssue, TValue> , provided by the GmanaValidationResultX extensionnullon success. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
orElse(
Either< L, R> f(L left)) → Either<L, R> -
Returns this Either when it is a Right, otherwise the result of
f. -
orElseWith<
L2> (Either< L2, R> f(L left)) → Either<L2, R> - Like orElse but the recovery may produce a different Left type.
-
recover(
R f(L left)) → Either< L, R> - Turns a Left into a Right by computing a fallback value.
-
rightOrNull(
) → R? -
Returns the Right value if this is a Right, otherwise
null. -
swap(
) → Either< R, L> - Swaps the sides of this Either, turning Left into Right and vice versa.
-
tap(
void f(R right)) → Either< L, R> -
Runs
fwith the Right value, if present, and returns this Either. -
tapLeft(
void f(L left)) → Either< L, R> -
Runs
fwith the Left value, if present, and returns this Either. -
toEmailDomainResult(
) → Either< EmailError, Email> -
Available on Either<
Converts this validation result to a domain Email result with EmailError.EmailValidationIssue, String> , provided by the EmailValidationAdapterX extension -
toEmailValueObject(
) → Either< EmailValidationIssue, Email> -
Available on Either<
Converts a successful validation into an Email value object.EmailValidationIssue, String> , provided by the EmailValidationAdapterX extension -
toIdentifierDomainResult(
) → Either< IdentifierError, IdentifierValue> -
Available on Either<
Converts this validation result to a domain IdentifierValue result with IdentifierError.IdentifierValidationIssue, String> , provided by the IdentifierValidationAdapterX extension -
toIdentifierValueObject(
) → Either< IdentifierValidationIssue, IdentifierValue> -
Available on Either<
Converts a successful validation into an IdentifierValue value object.IdentifierValidationIssue, String> , provided by the IdentifierValidationAdapterX extension -
toList(
) → List< R> - A single-element list for a Right, or an empty list for a Left.
-
toNetworkAddressDomainResult(
) → Either< NetworkAddressError, NetworkAddressValue> -
Available on Either<
Converts this validation result to a domain NetworkAddressValue result with NetworkAddressError.NetworkValidationIssue, String> , provided by the NetworkValidationAdapterX extension -
toNetworkAddressValueObject(
) → Either< NetworkValidationIssue, NetworkAddressValue> -
Available on Either<
Converts a successful validation into a NetworkAddressValue value object.NetworkValidationIssue, String> , provided by the NetworkValidationAdapterX extension -
toNumberDomainResult(
) → Either< NumberError, NumberValue> -
Available on Either<
Converts this validation result to a domain NumberValue result with NumberError.NumberValidationIssue, num> , provided by the NumberValidationAdapterX extension -
toNumberValueObject(
) → Either< NumberValidationIssue, NumberValue> -
Available on Either<
Converts a successful validation into a NumberValue value object.NumberValidationIssue, num> , provided by the NumberValidationAdapterX extension -
toOption(
) → Option< R> - Discards the Left value, keeping only whether a Right was present.
-
toPasswordDomainResult(
) → Either< PasswordError, Password> -
Available on Either<
Converts this validation result to a domain Password result with PasswordError.PasswordValidationIssue, String> , provided by the PasswordValidationAdapterX extension -
toPasswordValueObject(
) → Either< PasswordValidationIssue, Password> -
Available on Either<
Converts a successful validation into a Password value object.PasswordValidationIssue, String> , provided by the PasswordValidationAdapterX extension -
toPhoneDomainResult(
) → Either< PhoneError, PhoneValue> -
Available on Either<
Converts this validation result to a domain PhoneValue result with PhoneError.PhoneValidationIssue, String> , provided by the PhoneValidationAdapterX extension -
toPhoneValueObject(
) → Either< PhoneValidationIssue, PhoneValue> -
Available on Either<
Converts a successful validation into a PhoneValue value object.PhoneValidationIssue, String> , provided by the PhoneValidationAdapterX extension -
toResult(
) → GResult< R, L> -
Available on Either<
Converts this Either to a GResult, mapping Left to GFailure and Right to GSuccess.L, R> , provided by the GmanaEitherToResultX extension -
toString(
) → String -
A string representation of this object.
inherited
-
toTextDomainResult(
) → Either< TextError, TextValue> -
Available on Either<
Converts this validation result to a domain TextValue result with TextError.TextValidationIssue, String> , provided by the TextValidationAdapterX extension -
toTextValueObject(
) → Either< TextValidationIssue, TextValue> -
Available on Either<
Converts a successful validation into a TextValue value object.TextValidationIssue, String> , provided by the TextValidationAdapterX extension -
toUrlDomainResult(
) → Either< UrlError, UrlValue> -
Available on Either<
Converts this validation result to a domain UrlValue result with UrlError.UrlValidationIssue, Uri> , provided by the UrlValidationAdapterX extension -
toUrlValueObject(
) → Either< UrlValidationIssue, UrlValue> -
Available on Either<
Converts a successful validation into an UrlValue value object.UrlValidationIssue, Uri> , provided by the UrlValidationAdapterX extension -
zip<
R2> (Either< L, R2> other) → Either<L, (R, R2)> -
Pairs the Right values of this and
other. -
zipWith<
R2, R3> (Either< L, R2> other, R3 combine(R first, R2 second)) → Either<L, R3> -
Combines the Right values of this and
otherthroughcombine.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
cond<
L, R> (bool test, L ifFalse(), R ifTrue()) → Either< L, R> -
Builds a Right from
ifTruewhentestholds, otherwise a Left fromifFalse. -
fromNullable<
L, R> (R? value, L onNull()) → Either< L, R> -
Lifts a nullable
valueinto an Either, usingonNullfor the Left side. -
sequence<
L, R> (Iterable< Either< items) → Either<L, R> >L, List< R> > -
Collapses
itemsinto a single Either holding every Right value. -
traverse<
L, R, R2> (Iterable< R> items, Either<L, R2> f(R item)) → Either<L, List< R2> > -
Applies
fto every element ofitemsand sequences the results. -
tryCatch<
L, R> (R fn(), L onError(Object error, StackTrace stackTrace)) → Either< L, R> -
Executes
fnand catches any exception, returning Right on success or Left(onError(error, stackTrace)) on failure. -
tryCatchAsync<
L, R> (FutureOr< R> fn(), L onError(Object error, StackTrace stackTrace)) → Future<Either< L, R> > -
Asynchronously executes
fnand catches any exception, returning Right on success or Left(onError(error, stackTrace)) on failure.