在 flutter 中公开所有 RevenueCat PurchasesErrorCode 代码

Exposing all RevenueCat PurchasesErrorCode codes in flutter

我需要在我的 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 中获取它们 - 我错过了什么?

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

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

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

太糟糕了,我们不能只传递一个 int 作为第一个参数,我们必须传递一个 String,我想我们可以在 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;和一个 underlyingErrorMessage,希望可以帮助您调试任何问题。

希望对你有所帮助

上一个回答提到的问题已经解决,1.0.0版本以后可以这样处理:

try {
    PurchaserInfo purchaserInfo = await Purchases.purchasePackage(package);
} on PlatformException catch (e) {
    var errorCode = PurchasesErrorHelper.getErrorCode(e);

    if (errorCode == PurchasesErrorCode.purchaseCancelledError) {
      print("User cancelled");
    } else if (errorCode == PurchasesErrorCode.purchaseNotAllowedError) {
      print("User not allowed to purchase");
    }
}