0

I'm refactoring in_app_purchases and I'm trying to get the past purchases. According to the documentation:

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

But the example they provide doesn't get the past purchases, it adds the one you buy at that moment.

I moved from this:

final QueryPurchaseDetailsResponse purchaseResponse =
        await _connection.queryPastPurchases();

to this:

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

print(purchaseUpdated.toList());

I tried the above but the list is empty and for sure my user has purchases as I can show here when I try to buy the same version I bought before: enter image description here

How could get a List from previous purchases?

Dani
  • 3,128
  • 2
  • 43
  • 91

1 Answers1

0

You have to listen to the purchaseStream like this code in the example:

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

All the purchased items will be added to this stream, so you need to add all the results to your list like below:

    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.
    });

Now you can use purchasedList as previous purchases. By the way, all the newly purchased items will be also added to that stream and purchasedList.

UPDATE: After the above steps, you need to call _inAppPurchase.restorePurchases() to get the previous purchases, all the previous purchases will be added to purchasedList by the purchaseStream.

Lam Thanh Nhan
  • 486
  • 4
  • 5
  • I also tried that but it doesn't go in when I declare it. It liste for future purchases and I need to get the previous ones – Dani Sep 10 '21 at 08:04
  • The `purchaseStream` will listen to the previous and future purchases as mentioned in [this](https://github.com/flutter/plugins/blob/d5b65742487f64166a73f116b925ce800c45dcd7/packages/in_app_purchase/in_app_purchase/lib/in_app_purchase.dart#L189-L211) @Dani – Lam Thanh Nhan Sep 10 '21 at 11:24
  • btw, you know how can I wait for the purchases? I don't see how to make that function async – Dani Sep 10 '21 at 18:58
  • I mean, I know it's a Stream but I'd need to get the past purchases from a Future when the app loads to perform other actions as I was doing before with old version of the package: await this.inAppPurchase.queryPastPurchases(); – Dani Sep 10 '21 at 19:12
  • I think the old way is not possible with `stream` method. Btw, you shouldn't use that way to restore your previous purchases because AppStore will reject them. Instead, you should create a Restore button and call `.restorePurchases` when the user taps it. The previous purchases will be persisted automatically by the package, so you don't need to call `.restorePurchases` after it has been restored, it will be added to the `purchaseStream` in the next it's opened. – Lam Thanh Nhan Sep 11 '21 at 04:02
  • ok so that means I need to refactor my logic, just checking. Thanks again – Dani Sep 11 '21 at 08:03
  • any chance you could also know about this one? https://stackoverflow.com/questions/69150775/flutter-get-past-purchases-on-ios – Dani Sep 12 '21 at 11:33
  • 1
    You can check your post. I have answered. – Lam Thanh Nhan Sep 12 '21 at 12:24