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

如何将 fold 方法用于未来类型?

如何解决如何将 fold 方法用于未来类型?

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

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'

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。