Flutter:获取过去在 Android 上的购买记录

Flutter: get past purchases on Android

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

The InAppPurchaseConnection.queryPastPurchases method has been removed. Instead, you should use InAppPurchase.restorePurchases. This method emits each restored purchase on the InAppPurchase.purchaseStream, the PurchaseDetails object will be marked with a status of PurchaseStatus.restored

但是他们提供的例子没有得到过去的购买,它添加了你当时购买的。

我是从这里搬过来的:

final QueryPurchaseDetailsResponse purchaseResponse =
        await _connection.queryPastPurchases();

对此:

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

print(purchaseUpdated.toList());

我尝试了上面的方法,但列表是空的,并且当我尝试购买我之前购买的相同版本时,可以肯定我的用户已经购买了,因为我可以在这里显示:

如何从以前的购买中获得 List

您必须 listenpurchaseStream 就像示例中的 this code:

    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.