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);
  }
}
Implementers

Constructors

Either()
Creates an Either instance.
const

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

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.
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.
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.
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.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
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.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited