扑:storekit_duplicate_product_object。同一产品标识符有待处理的交易

Flutter: storekit_duplicate_product_object. There is a pending transaction for the same product identifier

我使用的是内购官方插件。但是当我尝试在 iOS 上购买订阅时,我看到了以下错误:

Unhandled Exception: PlatformException(storekit_duplicate_product_object, There is a pending transaction for the same product identifier. Please either wait for it to be finished or finish it manually using `completePurchase` to avoid edge cases., {applicationUsername: null, requestData: null, quantity: 1, productIdentifier: in_app_test, simulatesAskToBuyInSandbox: false}, null)

我正在使用 in_app_purchase 包的 in_app_purchase: ^0.5.2 版本

我调用它的代码如下所示:

   TextButton(
      child: Text(productDetails.price),
      style: flatButtonStyle,
      onPressed: () async {
        PurchaseParam purchaseParam = PurchaseParam(
            productDetails: productDetails,
            applicationUserName: null,
            sandboxTesting: false);
        if (productDetails.id == _kConsumableId) {
          _connection.buyConsumable(
              purchaseParam: purchaseParam,
              autoConsume: kAutoConsume || Platform.isIOS);
        } else {
          _connection.buyNonConsumable(
              purchaseParam: purchaseParam);

        }},
    )

请跟进#60763

您可以使用 SKPaymentQueueWrapper().finishTransaction(skPaymentTransactionWrapper)

完成这些交易

为避免此错误,请将下面的代码放在 buyNonConsumable 之前并运行:

var transactions = await SKPaymentQueueWrapper().transactions();
 transactions.forEach((skPaymentTransactionWrapper) {
    SKPaymentQueueWrapper().finishTransaction(skPaymentTransactionWrapper);
});

完整示例

TextButton(
  child: Text(productDetails.price),
  style: flatButtonStyle,
  onPressed: () async {
    PurchaseParam purchaseParam = PurchaseParam(
        productDetails: productDetails,
        applicationUserName: null,
        sandboxTesting: false);
    if (productDetails.id == _kConsumableId) {
      _connection.buyConsumable(
          purchaseParam: purchaseParam,
          autoConsume: kAutoConsume || Platform.isIOS);
    } else {
      _connection.buyNonConsumable(
          purchaseParam: purchaseParam);
      var transactions = await SKPaymentQueueWrapper().transactions();
      transactions.forEach((skPaymentTransactionWrapper) {
        SKPaymentQueueWrapper().finishTransaction(skPaymentTransactionWrapper);
      });
    }
  },
)

答案来自siloebb's answer

即使在 PurchaseStatus.error

之后,您也必须调用 .completePurchase()