flutter - 在颤动中公开所有 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 中获取它们——我错过了什么?
解决方案
我开发了 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
, 希望可以帮助您调试任何问题。
我希望这会有所帮助
推荐阅读
- windows - 在文件中搜索三个连续的文本行
- java - 使用包含运算符的字符串作为 if 语句中的变量
- c# - 将 ASP.NET 测试启动类移到项目根目录之外?
- json - 如何在 Golang 服务器中通过 ID 参数返回特定的 JSON 数据
- symfony - 如何仅在嵌入式表单中呈现特定的孙子?
- firebase - 在 NoSQL(Firestore)中检查现有相关文档的有效方法?
- c# - 在扩展方法中使用 Array 和 IEnumerable
- php - 比较控制器上的字符串
- javascript - 如何在 PHP 文件中捕获签名板图像并输出到 HTML 文件(如收据)中,然后将页面存储为 JPEG 或 PDF
- c# - wpf 集合子属性数据绑定路径