微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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 举报,一经查实,本站将立刻删除。