Rust-style Result type for type-safe error handling with pattern matching
dart pub add philiprehberger_result_typeRust-style Result type for type-safe error handling with pattern matching.
dependencies:
philiprehberger_result_type: ^0.1.0
dart pub get
import 'package:philiprehberger_result_type/philiprehberger_result_type.dart';
Result<int, String> divide(int a, int b) {
if (b == 0) return Result.err('Division by zero');
return Result.ok(a ~/ b);
}
final result = divide(10, 2);
final message = result.when(
ok: (value) => 'Result: $value',
err: (error) => 'Error: $error',
);
print(message); // Result: 5
// Map success values
final doubled = Result<int, String>.ok(5).map((v) => v * 2);
print(doubled); // Ok(10)
// Chain operations
final chained = Result<int, String>.ok(10)
.flatMap((v) => v > 0 ? Result.ok(v) : Result.err('must be positive'));
// Transform errors
final mapped = Result<int, String>.err('not found')
.mapErr((e) => 'Error: $e');
final result = await Result.tryAsync(
() async => fetchDataFromApi(),
(error, stack) => 'Request failed: $error',
);
final results = [Result<int, String>.ok(1), Result.ok(2), Result.ok(3)];
final collected = Result.collect(results);
print(collected); // Ok([1, 2, 3])
final mixed = [Result<int, String>.ok(1), Result.err('bad')];
final failed = Result.collect(mixed);
print(failed); // Err(bad)
final ok = Result<int, String>.ok(42);
print(ok.unwrap()); // 42
print(ok.unwrapOr(0)); // 42
final err = Result<int, String>.err('fail');
print(err.unwrapOr(0)); // 0
// err.unwrap(); // throws StateError
| Member | Description |
|---|---|
Result.ok(T value) | Create a success result |
Result.err(E error) | Create a failure result |
isOk | Whether this is an Ok result |
isErr | Whether this is an Err result |
okOrNull | Success value or null |
errOrNull | Error value or null |
unwrap() | Get value or throw StateError |
unwrapOr(T default) | Get value or return default |
map(U Function(T) f) | Transform success value |
mapErr(F Function(E) f) | Transform error value |
flatMap(Result<U,E> Function(T) f) | Chain Result-producing operation |
when({ok, err}) | Exhaustive pattern match |
Result.tryAsync(fn, onError) | Wrap async into Result |
Result.collect(results) | Combine list of Results |
Success variant. Holds a value of type T. All transformation methods operate on the value.
Failure variant. Holds an error of type E. Transformation methods like map and flatMap pass through the error unchanged.
dart pub get
dart analyze --fatal-infos
dart test
MIT - see LICENSE for details.