[英]How to extract Left or Right easily from Either type in Dart (Dartz)
I am looking to extract a value easily from a method that return a type Either<Exception, Object>
.我希望从返回类型Either<Exception, Object>
的方法中轻松提取值。
I am doing some tests but unable to test easily the return of my methods.我正在做一些测试,但无法轻松测试我的方法的返回。
For example:例如:
final Either<ServerException, TokenModel> result = await repository.getToken(...);
To test I am able to do that为了测试我能做到
expect(result, equals(Right(tokenModelExpected))); // => OK
Now how can I retrieve the result directly?现在如何直接检索结果?
final TokenModel modelRetrieved = Left(result); ==> Not working..
I found that I have to cast like that:我发现我必须这样投:
final TokenModel modelRetrieved = (result as Left).value; ==> But I have some linter complain, that telling me that I shouldn't do as to cast on object...
Also I would like to test the exception but it's not working, for example:我也想测试异常但它不起作用,例如:
expect(result, equals(Left(ServerException()))); // => KO
So I tried this所以我试过了
expect(Left(ServerException()), equals(Left(ServerException()))); // => KO as well, because it says that the instances are different.
Ok here the solutions of my problems:好的,我的问题的解决方案:
To extract/retrieve the data提取/检索数据
final Either<ServerException, TokenModel> result = await repository.getToken(...);
result.fold(
(exception) => DoWhatYouWantWithException,
(tokenModel) => DoWhatYouWantWithModel
);
//Other way to 'extract' the data
if (result.isRight()) {
final TokenModel tokenModel = result.getOrElse(null);
}
To test the exception测试异常
//You can extract it from below, or test it directly with the type
expect(() => result, throwsA(isInstanceOf<ServerException>()));
Another way to extract the value is simply to convert to Option
, then to a dart
nullable:提取值的另一种方法是简单地转换为Option
,然后转换为dart
可为空的:
final Either<Exception, String> myEither = Right("value");
final String? myValue = myEither.toOption().toNullable();
If you like you can define a simple extension to shortcut this:如果你喜欢,你可以定义一个简单的扩展来快捷方式:
extension EitherHelpers<L, R> on Either<L, R> {
R? unwrapRight() {
return toOption().toNullable();
}
}
Friends,朋友们,
just create dartz_x.dart like this one.只需像这样创建dartz_x.dart 。
import 'package:dartz/dartz.dart';
extension EitherX<L, R> on Either<L, R> {
R asRight() => (this as Right).value; //
L asLeft() => (this as Left).value;
}
And use like that.并像那样使用。
import 'dartz_x.dart';
void foo(Either<Error, String> either) {
if (either.isLeft()) {
final Error error = either.asLeft();
// some code
} else {
final String text = either.asRight();
// some code
}
}
Future<Either<Failure, FactsBase>> call(Params params) async {
final resulting = await repository.facts();
return resulting.fold(
(failure) {
return Left(failure);
},
(factsbase) {
DateTime cfend = sl<EndDateSetting>().finish;
List<CashAction> actions = factsbase.transfers.process(facts: factsbase, startDate: repository.today, finishDate: cfend); // process all the transfers in one line using extensions
actions.addAll(factsbase.transactions.process(facts: factsbase, startDate: repository.today, finishDate: cfend));
for(var action in actions) action.account.cashActions.add(action); // copy all the CashActions to the Account.
for(var account in factsbase.accounts) account.process(start: repository.today);
return Right(factsbase);
},
);
} }
extension EitherExtension<L, R> on Either<L, R> {
R? getRight() => fold<R?>((_) => null, (r) => r);
L? getLeft() => fold<L?>((l) => l, (_) => null);
}
I return fold result on Either type我返回任何类型的折叠结果
@override
Future<Either<Failure, bool>> call(UpdateReceiptParams params) async {
Either<Failure, ProcurementStore> procurementStore =
await _getProcurementStore();
return procurementStore.fold((_) => Left(ServerFailure()),
(procurementlStore) async {
List<ReceiptItem> receiptItems =
_getReceiptItem(params.shipmentStatus, params.quantityAmounts);
final Receipt receipt = Receiplt(
procurementStore: procurementStore,
costCenter: const CostCenter(),
receiptItems: receiptItems,
shipmentId: params.shipmentStatus.Id,
receiptType: params.shipmentStatus.ShipmentType,
description: params.description,
operationPeriodId: 0,
receiptDate: params.date,
receiptTime: params.time);
return await shipmentsRepository.updateReceipt(receipt);
});
}
To extract left or right:要提取左侧或右侧:
Left:剩下:
print(result.fold((l) => l, (r) => null))
Right:正确的:
print(result.fold((l) => null, (r) => r))
You can cast it but make sure u use isLeft / isRight first您可以投射它,但请确保您先使用 isLeft / isRight
final workflowUnload = await someEither();
if (workflowUnload.isLeft()) {
emit(state.copyWith.call(
status: JobOrderStatus.error,
failure: (workflowUnload as Left).value,
));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.