Flutter 如何在颤振中将其中一个的右侧转换为泛型
我有一个Task的扩展(来自dartz包),它看起来像这样Flutter 如何在颤振中将其中一个的右侧转换为泛型,flutter,dart,dartz,null-safety,Flutter,Dart,Dartz,Null Safety,我有一个Task的扩展(来自dartz包),它看起来像这样 extension TaskX<T extends Either<Object, U>, U> on Task<T> { Task<Either<Failure, U>> mapLeftToFailure() { return this.map( // Errors are returned as objects (either) =>
extension TaskX<T extends Either<Object, U>, U> on Task<T> {
Task<Either<Failure, U>> mapLeftToFailure() {
return this.map(
// Errors are returned as objects
(either) => either.leftMap((obj) {
try {
// So we cast them into a Failure
return obj as Failure;
} catch (e) {
// If it's an unexpected error (not caught by the service)
// We simply rethrow it
throw obj;
}
}),
);
}
}
任务上的扩展任务X{
任务F失败(){
把这个还给我(
//错误作为对象返回
(任择)=>任择.leftMap((obj){
试一试{
//所以我们让他们失败了
返回obj作为失败;
}捕获(e){
//如果是意外错误(服务未捕获)
//我们只是把它重新命名
抛出obj;
}
}),
);
}
}
此扩展的目标是返回类型为或
在我将颤振项目切换到空安全之前,这一切都很顺利。
现在,由于某种原因,返回类型为或
在我的代码中,看起来是这样的:
await Task(
() => _recipesService.getProduct(),
)
.attempt() // Attempt to run the above code, and catch every exceptions
.mapLeftToFailure() // this returns Task<Either<Failure, dynamic>>
.run()
.then(
(product) => _setProduct(product), // Here I get an error because I expect a type of Either<Failure, Product>
);
等待任务(
()=>\u recipesService.getProduct(),
)
.trunt()//尝试运行上述代码,并捕获所有异常
.mapLeftToFailure()//此操作返回任务
.run()
.那么(
(product)=>\u setProduct(product),//这里我得到了一个错误,因为我希望
);
我的问题是,我如何将其中一个的右侧转换为正确的类型