resultify 0.1.1 resultify: ^0.1.1 copied to clipboard
A Dart package for result-oriented programming, providing Result<R, E> type with convenient methods for streamlined error handling.
example/resultify_example.dart
import 'package:resultify/resultify.dart';
void main() {
standardUsage();
functionWrapping();
}
/// Standard usage of Result.
///
/// Here is shown how to use the Result type in a "standard" way.
void standardUsage() {
// Declare a function that returns a Result.
Result<int, String> divide(int a, int b) {
if (b == 0) return Result.error("Cannot divide by zero");
return Result.success(a ~/ b);
}
final result = divide(5, 3);
// You can extract the result or a default value.
final resultOrDefault = result.getResultOrDefault(defaultValue: 0);
print(resultOrDefault); // Prints "1".
// You can do the same thing with error.
final errorOrDefault = result.getErrorOrDefault(defaultValue: "No errors :)");
print(errorOrDefault); // Prints "No errors :)".
// Do something based on result match.
result.match(
onSuccess: print,
onError: print,
);
}
/// Function wrapping.
///
/// Function wrapping consists in running a function in a try-catch block.
/// The `wrap` functions handles exceptions automatically and
/// can map the exception to any other object as you prefer.
void functionWrapping() {
String successFunction() => "Success!";
String errorFunction() => throw "Error!";
final successResult = Result.wrap(successFunction, errorMapper: (e) => null);
// Prints 'Success!'
print(successResult.getResultOrDefault());
final errorResult = Result.wrap(errorFunction, errorMapper: (e) => e);
// Prints 'Error!'
print(errorResult.getErrorOrDefault());
}