mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-02-12 15:05:29 +00:00
53 lines
1.3 KiB
Dart
53 lines
1.3 KiB
Dart
|
/// Utility class that simplifies handling errors.
|
||
|
///
|
||
|
/// Return a [Result] from a function to indicate success or failure.
|
||
|
///
|
||
|
/// A [Result] is either an [Ok] with a value of type [T]
|
||
|
/// or an [Error] with an [Exception].
|
||
|
///
|
||
|
/// Use [Result.ok] to create a successful result with a value of type [T].
|
||
|
/// Use [Result.error] to create an error result with an [Exception].
|
||
|
///
|
||
|
/// Evaluate the result using a switch statement:
|
||
|
/// ```dart
|
||
|
/// switch (result) {
|
||
|
/// case Ok(): {
|
||
|
/// print(result.value);
|
||
|
/// }
|
||
|
/// case Error(): {
|
||
|
/// print(result.error);
|
||
|
/// }
|
||
|
/// }
|
||
|
/// ```
|
||
|
sealed class Result<T> {
|
||
|
const Result();
|
||
|
|
||
|
/// Creates a successful [Result], completed with the specified [value].
|
||
|
const factory Result.ok(T value) = Ok._;
|
||
|
|
||
|
/// Creates an error [Result], completed with the specified [error].
|
||
|
const factory Result.error(Exception error) = Error._;
|
||
|
}
|
||
|
|
||
|
/// A successful [Result] with a returned [value].
|
||
|
final class Ok<T> extends Result<T> {
|
||
|
const Ok._(this.value);
|
||
|
|
||
|
/// The returned value of this result.
|
||
|
final T value;
|
||
|
|
||
|
@override
|
||
|
String toString() => 'Result<$T>.ok($value)';
|
||
|
}
|
||
|
|
||
|
/// An error [Result] with a resulting [error].
|
||
|
final class Error<T> extends Result<T> {
|
||
|
const Error._(this.error);
|
||
|
|
||
|
/// The resulting error of this result.
|
||
|
final Exception error;
|
||
|
|
||
|
@override
|
||
|
String toString() => 'Result<$T>.error($error)';
|
||
|
}
|