- Rust-inspired Result class in Dart with 2 options:
Ok
orErr
with intuitive methods for asynchronous programming. - Extensions for duration
A simple usage example:
import 'package:rusted/rusted.dart';
import 'package:http/http.dart';
void main() {
Result<String, Exception> result = Ok('hi');
result.fold(
(ok) => print(ok),
(err) => print(err),
);
}
class MyCubit extends Cubit<MyState> {
MyCubit(this.myRepo) : super(MyInitialState());
final MyRepository myRepo;
void login() async {
final result = await myRepo.login();
result.fold(
(ok) => emit(MyLoggedInState(ok)),
(err) => emit(MyFailedLoginState(err)),
);
}
}
class MyRepository {
Future<Result<String, http.ClientException>> login() {
return Result.of(() async {
var res = http.get(URL);
return res.body['username'];
});
}
}
Duration(seconds: 5) == 5.seconds; //true
Duration(milliseconds: 250) == 250.milliseconds; // true
Duration(minutes: 1, seconds: 30) == 1.5.minutes; // true
Currently autocomplete and import doesn't work on extension methods, but typing the respective classes (IntTime
or DoubleTime
) will import it.
Since Dart doesn't provide a class similiar to Rust's Result, this package was made to fill that purpose.
A result is especially useful in situations where catching errors isn't desirable, such as in the mapEventToState
function of a Bloc, when it is much better to handle errors in the repository, making refactoring of the Bloc easier.
With Result
it is easier to deliver errors to the UI while forcing you to handle them.
Dartz is a great functional programming package which uses an Either class. Unfortunately it is not documented at all and can be a bit confusing.
-
When Type aliases land in Dart, create aliases for common types, for example
typedef ResultEx<T> = Result<T, Exception>
Please file feature requests and bugs at the issue tracker.