完成购买后,我收到开发人员错误,例如响应(应用内购买)

On complete purchase i'm getting developer error such as response (In app purchase )

BillingResponse.developerError I/flutter ( 5091): 购买处于无效状态。

有时我会收到此错误,但我遇到过计费响应正常的情况 (BillingResponse.ok)。可能是什么问题?我的代码遵循 flutter.dev 的官方示例,即 https://pub.dev/packages/in_app_purchase 这段代码之前一切都很好

var completePurchase = await InAppPurchaseConnection.instance
              .completePurchase(purchaseDetails);

哪个 returns BillingResponse.developerError 或有时魔法 returns BillingResponse.ok

在小部件中,我只调用 buyNonConsumable

  instance.buyNonConsumable(prod);

是否有可能是因为它未在生产中或其他与内部测试相关的原因,因为该应用程序正在进行内部测试? 总之,代码如下所示

     _subscription = getPurchaseStream.listen(
        (purchaseDetailsList) {
          _listenToPurchaseUpdated(purchaseDetailsList);
        }, onDone: () {
      _subscription.cancel();
      }, onError: (error) {
        // handle error here.
        print('Subscription error: $error');
      });

    _isAvailable = await _connection.isAvailable();
    if(!_isAvailable) {
      print('The store is not available, try again later..');
      return;
    }
    _myProductsIds = await _categoryRepository.getCategoriesGoogleProductsIds(); 
    await _getProducts();
    await _getPastPurchases();
    for (PurchaseDetails purchase in _purchases) {
      verifyPurchase(purchase.productID);
      if (_isPurchased) {  //Check if product is purchased
        await deliverProduct(); // Give the products on the user
      } else {
         _handleInvalidPurchase(purchase);
      }
    }
 purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
      if (purchaseDetails.status == PurchaseStatus.pending) { 
        print('Pending..');
      } else {
        if (purchaseDetails.status == PurchaseStatus.error) {
         print('To purchases status error: ${purchaseDetails.error}');
        } else if (purchaseDetails.status == PurchaseStatus.purchased) {
          verifyPurchase(purchaseDetails.productID); // change value of _isPurchased
          if (_isPurchased) {
            _purchases.add(purchaseDetails);
     
            for(String id in _myProductsIds) {
              if (purchaseDetails.productID == id) {
                await deliverProduct(); // Give the products on the user
              }
            }
          } else {
            _handleInvalidPurchase(purchaseDetails);
            return;
          }
        }
        if (Platform.isAndroid) {
          for(String id in _myProductsIds) {
            if (purchaseDetails.productID == id) {
              await InAppPurchaseConnection.instance
                  .consumePurchase(purchaseDetails);
            }
          }
        }
        if (purchaseDetails.pendingCompletePurchase) {
          var completePurchase = await InAppPurchaseConnection.instance
              .completePurchase(purchaseDetails);
        }
      }

我不知道为什么在官方示例中有 consumePurchase,如果平台是 android,但由于这段代码,我的应用程序崩溃并返回错误 'Developer.error' 在重要的行中,它们是

var completePurchase = await InAppPurchaseConnection.instance
              .completePurchase(purchaseDetails);

我阅读了 completePurchase 方法的文档,我看到了重要的评论:

  /// The [consumePurchase] acts as an implicit [completePurchase] on Android.
  Future<BillingResultWrapper> completePurchase(PurchaseDetails purchase);

也许应用内购买的版本有更新,此后某个示例没有更新。

Link 例子:https://github.com/flutter/plugins/tree/master/packages/in_app_purchase/in_app_purchase/example