Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter in_app_purchase 0.2.1 purchaseUpdatedStream.listen not working on Android

I am able to buy a subscription on Android, and the subscription shows up in queryPastPurchases(), but the _listenToPurchaseUpdated() method is never triggered after buying the product. I know that this was a bug in previous releases of flutter in_app_purchase, but it was said to be fixed in 0.2.1. However it doesn't seem to work for me... Is there something wrong with my code?

  /// The In App Purchase plugin
  InAppPurchaseConnection _iap = InAppPurchaseConnection.instance

  /// Updates to purchases
  StreamSubscription<List<PurchaseDetails>> _subscription;

  @override
  initState() {

    final Stream purchaseUpdates = InAppPurchaseConnection.instance.purchaseUpdatedStream;
    _subscription = purchaseUpdates.listen((purchases) {
      _purchases.addAll(purchases);
      _listenToPurchaseUpdated(_purchases);
    });

    super.initState();
  }

  /// Get all products available for sale
  Future<void> _getProducts() async {
    Set<String> ids = Set.from(['subscription_product']);
    ProductDetailsResponse response = await _iap.queryProductDetails(ids);

    setState(() {
      _products = response.productDetails;
    });

    if(response.error != null && response.error.message != null){
      setState(() {
        _iapStoreProblem = response.error.message;
      });
    }
  }

  void _buyProduct(ProductDetails prod) {
    final PurchaseParam purchaseParam = PurchaseParam(productDetails: prod);
    _iap.buyNonConsumable(purchaseParam: purchaseParam);
  }
like image 642
Christer Avatar asked Feb 02 '26 10:02

Christer


1 Answers

I found the solution.

Turns out I had to use

StreamSubscription _subscription;

instead of

StreamSubscription<List<PurchaseDetails>> _subscription;

and then:

_subscription = _iap.purchaseUpdatedStream.listen((data) => setState(() {
    _purchases.addAll(data);
    _listenToPurchaseUpdated(data);
}));


void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
    purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
      ...
    });
}

Also, a small note: PurchaseDetails.status on Android is Null.

like image 183
Christer Avatar answered Feb 05 '26 01:02

Christer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!