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);
  }
}
Available extensions

Constructors

Either()
Creates an Either instance.
const

Properties

errorOrNull → E?

Available on Either<E, T>, provided by the GmanaValueObjectResultX extension

The validation error, or null when validation succeeded.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isInvalid → bool

Available on ValidationResult<TIssue, TValue>, provided by the GmanaValidationResultX extension

Whether validation failed.
no setter
isInvalid → bool

Available on Either<E, T>, provided by the GmanaValueObjectResultX extension

Whether validation failed.
no setter
issueOrNull → TIssue?

Available on ValidationResult<TIssue, TValue>, provided by the GmanaValidationResultX extension

The typed validation issue, or null when validation succeeded.
no setter
isValid → bool

Available on ValidationResult<TIssue, TValue>, provided by the GmanaValidationResultX extension

Whether validation succeeded.
no setter
isValid → bool

Available on Either<E, T>, provided by the GmanaValueObjectResultX extension

Whether validation succeeded.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
valueOrNull → TValue?

Available on ValidationResult<TIssue, TValue>, provided by the GmanaValidationResultX extension

The validated value, or null when validation failed.
no setter
valueOrNull → T?

Available on Either<E, T>, provided by the GmanaValueObjectResultX extension

The validated value, or null when validation failed.
no setter

Methods

all(bool test(R right)) → bool
Returns true when this is a Left or when test passes for the Right value.
bimap<L2, R2>(L2 ifLeft(L left), R2 ifRight(R right)) → Either<L2, R2>
Applies ifLeft or ifRight and returns a new Either with mapped values.
contains(R value) → bool
Returns true when this is a Right containing value.
exists(bool test(R right)) → bool
Returns true when this is a Right and test passes.
filterOrElse(bool test(R right), L onFalse(R right)) → Either<L, R>
Demotes a Right to a Left when test fails.
flatMap<R2>(Either<L, R2> f(R right)) → Either<L, R2>
Applies the function f to 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<L, R2>> f(R right)) → Future<Either<L, R2>>
Asynchronously applies f to 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 defaultValue when 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 true if this is a Left.
isRight() → bool
Returns true if 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<E, T>, provided by the GmanaValueObjectResultX extension

Resolves the validation error to a message, or returns null on success.
messageOrNull(ValidationMessageResolver<TIssue> resolve) → String?

Available on ValidationResult<TIssue, TValue>, provided by the GmanaValidationResultX extension

Resolves the validation issue to a message, or returns null on 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 f with the Right value, if present, and returns this Either.
tapLeft(void f(L left)) → Either<L, R>
Runs f with the Left value, if present, and returns this Either.
toEmailDomainResult() → Either<EmailError, Email>

Available on Either<EmailValidationIssue, String>, provided by the EmailValidationAdapterX extension

Converts this validation result to a domain Email result with EmailError.
toEmailValueObject() → Either<EmailValidationIssue, Email>

Available on Either<EmailValidationIssue, String>, provided by the EmailValidationAdapterX extension

Converts a successful validation into an Email value object.
toIdentifierDomainResult() → Either<IdentifierError, IdentifierValue>

Available on Either<IdentifierValidationIssue, String>, provided by the IdentifierValidationAdapterX extension

Converts this validation result to a domain IdentifierValue result with IdentifierError.
toIdentifierValueObject() → Either<IdentifierValidationIssue, IdentifierValue>

Available on Either<IdentifierValidationIssue, String>, provided by the IdentifierValidationAdapterX extension

Converts a successful validation into an IdentifierValue value object.
toList() → List<R>
A single-element list for a Right, or an empty list for a Left.
toNetworkAddressDomainResult() → Either<NetworkAddressError, NetworkAddressValue>

Available on Either<NetworkValidationIssue, String>, provided by the NetworkValidationAdapterX extension

Converts this validation result to a domain NetworkAddressValue result with NetworkAddressError.
toNetworkAddressValueObject() → Either<NetworkValidationIssue, NetworkAddressValue>

Available on Either<NetworkValidationIssue, String>, provided by the NetworkValidationAdapterX extension

Converts a successful validation into a NetworkAddressValue value object.
toNumberDomainResult() → Either<NumberError, NumberValue>

Available on Either<NumberValidationIssue, num>, provided by the NumberValidationAdapterX extension

Converts this validation result to a domain NumberValue result with NumberError.
toNumberValueObject() → Either<NumberValidationIssue, NumberValue>

Available on Either<NumberValidationIssue, num>, provided by the NumberValidationAdapterX extension

Converts a successful validation into a NumberValue value object.
toOption() → Option<R>
Discards the Left value, keeping only whether a Right was present.
toPasswordDomainResult() → Either<PasswordError, Password>

Available on Either<PasswordValidationIssue, String>, provided by the PasswordValidationAdapterX extension

Converts this validation result to a domain Password result with PasswordError.
toPasswordValueObject() → Either<PasswordValidationIssue, Password>

Available on Either<PasswordValidationIssue, String>, provided by the PasswordValidationAdapterX extension

Converts a successful validation into a Password value object.
toPhoneDomainResult() → Either<PhoneError, PhoneValue>

Available on Either<PhoneValidationIssue, String>, provided by the PhoneValidationAdapterX extension

Converts this validation result to a domain PhoneValue result with PhoneError.
toPhoneValueObject() → Either<PhoneValidationIssue, PhoneValue>

Available on Either<PhoneValidationIssue, String>, provided by the PhoneValidationAdapterX extension

Converts a successful validation into a PhoneValue value object.
toResult() → GResult<R, L>

Available on Either<L, R>, provided by the GmanaEitherToResultX extension

Converts this Either to a GResult, mapping Left to GFailure and Right to GSuccess.
toString() → String
A string representation of this object.
inherited
toTextDomainResult() → Either<TextError, TextValue>

Available on Either<TextValidationIssue, String>, provided by the TextValidationAdapterX extension

Converts this validation result to a domain TextValue result with TextError.
toTextValueObject() → Either<TextValidationIssue, TextValue>

Available on Either<TextValidationIssue, String>, provided by the TextValidationAdapterX extension

Converts a successful validation into a TextValue value object.
toUrlDomainResult() → Either<UrlError, UrlValue>

Available on Either<UrlValidationIssue, Uri>, provided by the UrlValidationAdapterX extension

Converts this validation result to a domain UrlValue result with UrlError.
toUrlValueObject() → Either<UrlValidationIssue, UrlValue>

Available on Either<UrlValidationIssue, Uri>, provided by the UrlValidationAdapterX extension

Converts a successful validation into an UrlValue value object.
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 other through combine.

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 ifTrue when test holds, otherwise a Left from ifFalse.
fromNullable<L, R>(R? value, L onNull()) → Either<L, R>
Lifts a nullable value into an Either, using onNull for the Left side.
sequence<L, R>(Iterable<Either<L, R>> items) → Either<L, List<R>>
Collapses items into 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 f to every element of items and sequences the results.
tryCatch<L, R>(R fn(), L onError(Object error, StackTrace stackTrace)) → Either<L, R>
Executes fn and 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 fn and catches any exception, returning Right on success or Left(onError(error, stackTrace)) on failure.