未来的类型如何使用折叠方法?

问题描述 投票:0回答:2

我将此方法作为我的存储库:

Future<Either<Exceptions, bool>> userRegister(FormData formData) async {
    try {
      final response = await _mourdakApi.userRegister(formData);
      bool _isSuccesed;
      if (response.statusCode == 200)
        _isSuccesed = true;
      else
        _isSuccesed = false;
      return Right(_isSuccesed);
    } catch (e) {
      return Left(
        Exceptions(
          message: e.toString(),
        ),
      );
    }
  }

现在我想在这里使用折叠方法:

final failedOrResult = _mourdakRepository.userRegister(event.formData);

return failedOrResult.fold(
      (exception) => UserExceptionState(exception.message),
      (isSuccesed) => UserRegisterState(isSuccesed));

但是我收到了这个错误:

The method 'fold' isn't defined for the type 'Future'
flutter future dartz
2个回答
0
投票

我不知道它是否还没有解决,但只是缺少等待。 另一种方法是使用 lib dartz 中定义的 Task 方法

final failedOrResult = await _mourdakRepository.userRegister(event.formData);

return failedOrResult.fold(
      (exception) => UserExceptionState(exception.message),
      (isSuccesed) => UserRegisterState(isSuccesed));

0
投票

是的。缺少正确的等待。现在正在工作。

© www.soinside.com 2019 - 2024. All rights reserved.