Last updated:
0 purchases
record result
Record Result #
Generated by the Very Good CLI 🤖
record_result is a Dart package that provides a type called Result for handling the outcomes of operations. It can represent either a successful result or a failure, allowing you to handle errors in a more functional and expressive way.
Getting Started 🎉 #
Add the following dependency to your pubspec.yaml file:
dependencies:
record_result: ^1.0.0
copied to clipboard
Then run:
dart pub get
copied to clipboard
Usage 😎 #
Basic Result Types #
Result
A Result is a type that represents either a success or a failure. It contains either a successful value (success) or an unsuccessful value represented by a Failure.
Example:
Result<int> result = right(42);
if (result.isSuccess) {
print('Operation succeeded with result: ${result.success}');
} else {
print('Operation failed with error: ${result.failure!.errorMessage}');
}
copied to clipboard
FutureResult
A FutureResult is a type that represents a Future of a Result. It is useful for working with asynchronous operations that may produce either a successful result or a failure.
Example:
FutureResult<int> futureResult = fetchSomeData();
futureResult.then((result) {
if (result.isSuccess) {
print('Operation succeeded with result: ${result.success}');
} else {
print('Operation failed with error: ${result.failure!.errorMessage}');
}
});
copied to clipboard
StreamResult
A StreamResult is a type that represents a Stream of a Result. It is useful for handling asynchronous streams of results, where each element can be either a success or a failure.
Example:
StreamResult<int> streamResult = fetchDataAsStream();
await for (Result<int> result in streamResult) {
if (result.isSuccess) {
print('Received successful result: ${result.success}');
} else {
print('Received failure: ${result.failure!.errorMessage}');
}
}
copied to clipboard
ResultVoid
A ResultVoid is a type that represents either a void success or a Failure. This type is useful when the operation doesn't produce a specific value, and success is indicated by the absence of a failure.
Example:
ResultVoid result = voidSuccess;
if (result.isSuccess) {
print('Operation succeeded!');
} else {
print('Operation failed with error: ${result.failure!.errorMessage}');
}
copied to clipboard
FutureResultVoid
A FutureResultVoid is a type that represents a Future of a ResultVoid. It is used for asynchronous operations that don't produce a specific value, and success is indicated by the absence of a failure.
Example:
FutureResultVoid futureResult = performAsyncOperation();
futureResult.then((result) {
if (result.isSuccess) {
print('Operation succeeded!');
else {
print('Operation failed with error: ${result.failure!.errorMessage}');
}
});
copied to clipboard
StreamResultVoid
A StreamResultVoid is a type that represents a Stream of a ResultVoid. It is used for handling asynchronous streams of results where each element can be either a void success or a failure.
Example:
StreamResultVoid streamResultVoid = fetchDataAsStream();
await for (ResultVoid result in streamResultVoid) {
if (result.isSuccess) {
print('Received successful result!');
} else {
print('Received failure: ${result.failure!.errorMessage}');
}
}
copied to clipboard
Additional Utility Methods 💪 #
Fold Extension #
The Fold extension provides additional utility methods for handling success and failure scenarios. This works on all Result types
Example:
Result<int> result = right(42);
String output = result.fold(
(success) => 'Success: $success',
(failure) => 'Failure: ${failure.errorMessage}',
);
print(output); // Output: Success: 42
copied to clipboard
right and left Functions #
The right and left functions are utility functions for creating instances of Result.
right
The right function creates a successful Result with the specified value.
Example:
Result<int> result = right(42);
copied to clipboard
left
The left function creates an unsuccessful result with the specified Failure.
Example:
final failure = Failure(message: 'Something went wrong', statusCode: 500);
Result<String> result = left(failure);
copied to clipboard
Contributing 🚀 #
If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.