如何在flutter中将Either的右侧转换为泛型类型

问题描述

我有一个 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) => 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;
        }
      }),);
  }
}

这个扩展的目标是返回一个 Either<Failure,U> 类型的值

这一切正常,直到我将我的颤振项目切换到空安全。 现在,出于某种原因,返回类型是 Either<Failure,dynamic>

在我的代码中,它看起来像这样:

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>
        );

我的问题是,我怎样才能将任何一个的右侧转换为正确的类型?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)