1

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

我的问题是,如何将 Either 的右侧转换为正确的类型?

4

0 回答 0