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
-
hashCode
→ int
-
The hash code for this object.
no setterinherited
-
runtimeType
→ Type
-
A representation of the runtime type of the object.
no setterinherited