Either<L, R> class abstract

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()
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

flatMap<R2>(Either<L, R2> f(R right)) Either<L, R2>
fold<B>(B ifLeft(L left), B ifRight(R right)) → B
getLeft() → L
getRight() → R
isLeft() bool
isRight() bool
map<R2>(R2 f(R right)) Either<L, R2>
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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