multiple_result 1.0.3 multiple_result: ^1.0.3 copied to clipboard
Multiple results for dart. Inspired by dartz's Either and Kotlin's sealed classes
multiple_result #
Result package for dart inspired by the work of dartz's Either and Kotlin's sealed classes.
This package is perfect to those of you who just want the Multiple results functionality from dartz. 👌
How to use it #
In the return of a function, set it to return a Result type;
Result getSomethingPretty();
then add the Error and the Success types.
Result<Exception, String> getSomethingPretty() {
}
in return of the function, you just need to return
return Success("Something Pretty");
or
return Error(Exception("something ungly happened..."));
The function should look something like this:
Result<Exception, String> getSomethingPretty() {
if(isOk) {
return Success("OK!");
} else {
return Error(Exception("Not Ok!"));
}
}
Handling the Result with when
void main() {
final result = getSomethingPretty();
result.when((error) {
// handle the error here
print(error);
}, (success) {
// handle the success here
print(success);
});
}
Handling the Result with get
void main() {
final result = getSomethingPretty();
String? mySuccessResult;
if (result.isSuccess()) {
mySuccessResult = result.get();
}
}