multiple_result 1.0.4
multiple_result: ^1.0.4 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. 👌
Old version:
If you're looking for a non null-safety version, you can find it in here
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 ugly 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();
final String message = result.when(
(error) {
// handle the error here
return "error";
}, (success) {
// handle the success here
return "success";
},
);
}
Handling the Result with get
void main() {
final result = getSomethingPretty();
String? mySuccessResult;
if (result.isSuccess()) {
mySuccessResult = result.get();
}
}
Handling the Result with getSuccess
void main() {
final result = getSomethingPretty();
String? mySuccessResult;
if (result.isSuccess()) {
mySuccessResult = result.getSuccess();
}
}
Handling the Result with getError
void main() {
final result = getSomethingPretty();
Exception? myException;
if (result.isError()) {
myException = result.getError();
}
}