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