https://github.com/leadcodedev/result_pattern
Provides a robust way of handling operations that may succeed or fail.
https://github.com/leadcodedev/result_pattern
error patterns result
Last synced: 4 months ago
JSON representation
Provides a robust way of handling operations that may succeed or fail.
- Host: GitHub
- URL: https://github.com/leadcodedev/result_pattern
- Owner: LeadcodeDev
- License: mit
- Created: 2024-06-24T10:01:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-25T22:20:26.000Z (over 1 year ago)
- Last Synced: 2025-03-06T00:36:09.308Z (12 months ago)
- Topics: error, patterns, result
- Language: Dart
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Result Pattern
The `Result Pattern` package for Dart provides a robust way of handling operations that may succeed or fail.
By encapsulating the result of an operation in a `Result` object, this package allows developers to write code that is
more predictable, safer, easier to understand and maintain.
## Benefits
- **Improved error handling**: Encapsulates success and failure results in a single, predictable type.
predictable type.
- **Cleaner code**: Reduces the need for try-catch blocks, making the code base cleaner and more readable.
- **Composable**: Makes it easy to chain several operations together and manage their results in a unified way.
## Installation
Add `result_pattern` to your `pubspec.yaml` file:
```bash
dart pub add result_pattern
```
## Utilisation
### Basique
```dart
import 'package:result_pattern/result_pattern.dart';
void main() {
final result = Result.ok(42);
expect(result.unwrap(), 42);
final value = switch (result) {
Ok(:final value) => value,
Err(:final error) => throw error,
};
expect(value, 42);
}
```
### Future usage
```dart
import 'package:result_pattern/result_pattern.dart';
AsyncResult getOkResult() => Result.ok(42);
Future main() async {
expect(await future.unwrap(), 42);
// Use pattern matching to extract the value or throw an error
final value = switch (await future) {
Ok(:final value) => value,
Err(:final error) => throw error,
};
// Use the match method to handle error cases
final value = await getOkResult()
.expect('Error message');
expect(value, 42);
}
```
## Contribution
Contributions are welcome!
Feel free to submit a pull request or open an issue if you have suggestions or find bugs.
## License
This package is licensed under the MIT license. See the LICENSE file for more details.