首页 > 解决方案 > 在颤动中公开所有 RevenueCat PurchasesErrorCode 代码

问题描述

我需要在我的 Flutter 应用程序中捕获所有列出的 PurchasesErrorCode 错误代码,以便我可以相应地响应它们。

目前我只能捕获“userCancelled”,对于其他一切我只能报告标准 PlatformException 代码、消息和详细信息属性中返回的信息,而不知道它们将包含什么。

try {

  // Code to make purchase..

} on PlatformException catch (e) {

  if (!(e.details as Map)["userCancelled"]) {

    // Here I need a comprehensive switch statement so I can
    // retry where appropriate/control what messages the user sees

    String reason = '';
    (e.details as Map).forEach((k,v) => reason += '$k => $v');
    showError(context, 'Error', '${e.code} : ${e.message}');

  } else {

    showError(context, 'Purchase Cancelled', 'Your purchase was not completed, you have not been charged.');

  }
}

这些代码在 IOS/Swift 和 Android/Kotlin 中公开,但我无法在 Flutter/Dart 中获取它们——我错过了什么?

标签: flutterrevenuecat

解决方案


我开发了 RevenueCat Flutter 插件,前段时间我在 GitHub 上创建了一个问题来跟踪它(https://github.com/RevenueCat/purchases-flutter/issues/3)。很抱歉,我们的 Flutter 错误处理还有一些改进的空间。

当我们发送平台异常时,我们将错误代码作为字符串传递:

result.error(error.getCode().ordinal() + "", error.getMessage(), userInfoMap);

太糟糕了,我们不能只传递一个 int 作为第一个参数,我们必须传递一个字符串,我想我们可以在userInfoMap. 但是现在,由于我们还没有为枚举提供错误代码,你必须在你的代码中做这样的事情:

enum PurchasesErrorCode {
  UnknownError,
  PurchaseCancelledError,
  StoreProblemError,
  PurchaseNotAllowedError,
  PurchaseInvalidError,
  ProductNotAvailableForPurchaseError,
  ProductAlreadyPurchasedError,
  ReceiptAlreadyInUseError,
  InvalidReceiptError,
  MissingReceiptFileError,
  NetworkError,
  InvalidCredentialsError,
  UnexpectedBackendResponseError,
  ReceiptInUseByOtherSubscriberError,
  InvalidAppUserIdError,
  OperationAlreadyInProgressError,
  UnknownBackendError,
  InsufficientPermissionsError
}

try {
} on PlatformException catch (e) {
  PurchasesErrorCode errorCode = PurchasesErrorCode.values[int.parse(e.code)];
  switch (errorCode) {
    case PurchasesErrorCode.UnknownError:
    case PurchasesErrorCode.PurchaseCancelledError:
    case PurchasesErrorCode.StoreProblemError:
    // Add rest of cases
  }
}

当您这样做时e.details,您还可以访问readableErrorCode包含错误代码名称的 a;和underlyingErrorMessage, 希望可以帮助您调试任何问题。

我希望这会有所帮助


推荐阅读