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 Success and the Error types.
Result<String, Exception> 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<String, Exception> 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";
},
);
}
final result = getSomethingPretty();
// notice the [onSuccess] or [onError] will only be executed if
// the result is a Success or an Error respectivaly.
final output = result.onSuccess((name) {
// handle here the success
return "";
});
final result = getSomethingPretty();
// [result] is NOT an Error, this [output] will be null.
final output = result.onError((exception) {
// handle here the error
return "";
});
note: [get] is now deprecated and will be removed in the next version.
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.tryGetSuccess();
}
}
void main() {
final result = getSomethingPretty();
Exception? myException;
if (result.isError()) {
myException = result.tryGetError();
}
}
void main() {
final result = getResult()
.map((e) => MyObject.fromMap(e));
result.tryGetSuccess(); //Instance of 'MyObject'
}
void main() {
final result = getResult()
.mapError((e) => MyException(e));
result.tryGetError(); //Instance of 'MyException'
}
Result<String, MyException> checkIsEven(String input){
if(input % 2 == 0){
return Success(input);
} else {
return Error(MyException('isn`t even!'));
}
}
void main() {
final result = getNumberResult()
.flatMap((s) => checkIsEven(s));
}
void main() {
final result = getSomethingPretty().pure(10);
String? mySuccessResult;
if (result.isSuccess()) {
mySuccessResult = result.tryGetSuccess(); // 10
}
}
Some results do not need a specific return. Use the Unit type to signal an empty return.
Result<Unit, Exception>
AsyncResult<S, E>
represents an asynchronous computation.
Use this component when working with asynchronous Result.
AsyncResult has some of the operators of the Result object to perform data transformations (Success or Error) before executing the Future.
The operators of the Result object available in AsyncResult are:
- map
- mapError
- flatMap
- pure
Use the run() method to run an AsyncResult<S, E>
turning it into a Result<S, E>
:
AsyncResult<String, Exception> fetch(){
return AsyncResult(() async {
await Future.delayer(Duration(second: 1));
return Success('Done!');
});
}
final result = await fetch().run();
//result now is Result<String, Exception>
There are 3 constructors for AsyncResult. Are they:
// default
AsyncResult(() async {
...
});
// with success value
AsyncResult.success(10);
// with error value
AsyncResult.error(MyException);