error_or 0.0.2+2 error_or: ^0.0.2+2 copied to clipboard
Return a result ErrorOr with either a value T or an error Object.
import 'dart:math';
import 'package:error_or/error_or.dart';
Future<ErrorOr<String>> getSuccessOrFailure() async {
await Future.delayed(Duration(milliseconds: 500));
if (Random().nextBool() == false) {
return ErrorOr.withError(Exception('Error'));
}
return ErrorOr.withValue('Success');
}
void main() async {
final errorOr = await getSuccessOrFailure();
if (errorOr.hasError) {
print(errorOr.error);
return;
}
print(errorOr.value);
}