【问题标题】:Dart - Why do I have to await this either.fold callDart - 为什么我必须等待这个 either.fold 调用
【发布时间】:2020-05-13 19:43:44
【问题描述】:
    Future<Either<Failure, User>> call(SignUpParams params) async {

    Either<Failure, User> failureOrUser;

    // Creates a User
    failureOrUser = await repository.signUpUser(
        params.email, params.password, params.type);

    // Creates a [Learner / Instructor] if User returned
    await failureOrUser.fold(
            (failure) => left(failure),
            (user) async {
              final remoteServerFailureOrSuccess =
                await createLearnerOrInstructor(CreateLOIParam(user: user));

              // check if [Learner / Instructor] creation has failed
              remoteServerFailureOrSuccess.fold(
                      (failure) => failureOrUser = left(failure),
                      (success) => null
              );

            }
    );

    return failureOrUser;
} 

我不明白为什么我需要在 failureOrUser.fold(); 方法前面放置一个 await

如果我不这样做,那么(user) async {} 方法就不会等待

final remoteServerFailureOrSuccess = await createLearnerOrInstructor(CreateLOIParam(user: user));

return failureOrUser;在之前被调用

remoteServerFailureOrSuccess.fold(
  (failure) => failureOrUser = left(failure),
  (success) => null
);

被调用。

我得到一个 'await 在 'Object' 之前应用,这不是 Future' 错误提示,但代码只等到整个方法完成后才返回,这样做是这样的。 p>

我试过了

await remoteServerFailureOrSuccess.fold(
 (failure) => failureOrUser = left(failure),
 (success) => null
);

但这仍然不起作用。

所以在我看来是这样的

final remoteServerFailureOrSuccess = await createLearnerOrInstructor(CreateLOIParam(user: user));

实际上并没有在等待。

【问题讨论】:

    标签: asynchronous flutter dart async-await


    【解决方案1】:

    好吧,我想我明白了,因为我设置了 (user) async {} 它使 fold 成为未来,但另一半不是异步 (failure) =&gt; left(failure) 所以我假设我得到了错误正因为如此。

    一旦我将失败设置为异步,错误就消失了。 (failure) async =&gt; left(failure)

    有点愚蠢的问题,但我会留下它,以防有人遇到同样的错误。

    【讨论】:

      猜你喜欢
      • 2022-12-03
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多