record_result 1.0.1+2 copy "record_result: ^1.0.1+2" to clipboard
record_result: ^1.0.1+2 copied to clipboard

record_result is a package that provides a type called Result for handling the outcomes of operations.

Record Result #

coverage style: very good analysis License: MIT

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

Then run:

dart pub get

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}');
}

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}');
  }
});

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}');
  }
}

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}');
}

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}');
  }
});

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}');
  }
}

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

right and left Functions #

The right and left functions are utility functions for creating instances of Result.

The right function creates a successful Result with the specified value.

Example:

Result<int> result = right(42);

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

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.

4
likes
140
pub points
24%
popularity

Publisher

unverified uploader

record_result is a package that provides a type called Result for handling the outcomes of operations.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

equatable

More

Packages that depend on record_result