0

我正在重构in_app_purchases,我正在尝试获取过去的购买。根据文档:

InAppPurchaseConnection.queryPastPurchases 方法已被删除。相反,您应该使用 InAppPurchase.restorePurchases。此方法在 InAppPurchase.purchaseStream 上发出每个恢复的购买,PurchaseDetails 对象将标记为 PurchaseStatus.restored 的状态

但是他们提供的示例并没有获得过去的购买,它会添加您当时购买的那个。

我从这个搬家:

final QueryPurchaseDetailsResponse purchaseResponse =
        await _connection.queryPastPurchases();

对此:

final Stream<List<PurchaseDetails>> purchaseUpdated = inAppPurchase.purchaseStream;

print(purchaseUpdated.toList());

我尝试了上述方法,但列表是空的,并且当我尝试购买之前购买的相同版本时,我可以在此处显示我的用户已购买: 在此处输入图像描述

怎么List能从以前的购买中得到一个?

4

1 回答 1

0

您必须在示例listenpurchaseStream喜欢此代码

    final Stream<List<PurchaseDetails>> purchaseUpdated =
        _inAppPurchase.purchaseStream;
    _subscription = purchaseUpdated.listen((purchaseDetailsList) {
      _listenToPurchaseUpdated(purchaseDetailsList);
    }, onDone: () {
      _subscription.cancel();
    }, onError: (error) {
      // handle error here.
    });

所有购买的商品都将添加到此流中,因此您需要将所有结果添加到您的列表中,如下所示:

    final List<PurchaseDetails> purchasedList = [];
    final Stream<List<PurchaseDetails>> purchaseUpdated =
        _inAppPurchase.purchaseStream;
    _subscription = purchaseUpdated.listen((purchaseDetailsList) {

      purchasedList.addAll(purchaseDetailsList);

    }, onDone: () {
      _subscription.cancel();
    }, onError: (error) {
      // handle error here.
    });

现在您可以purchasedList用作以前的购买。顺便说一句,所有新购买的商品也将添加到该流中,并且purchasedList.

更新:完成上述步骤后,您需要调用_inAppPurchase.restorePurchases()以获取以前的购买,所有以前的购买将被添加purchasedListpurchaseStream.

于 2021-09-10T04:32:52.623 回答