Flutter In App Purchase: InAppPurchaseConnection.instance.queryProductDetails returns empty list
Asked Answered
B

2

7

I'm following this video: https://www.youtube.com/watch?v=NWbkKH-2xcQ&t=495s and I'm using an Android device.

When I try to get the products from the google play console I'm getting an empty list in this section.

  Future<void> _getProducts() async {

    Set<String> ids = <String>['credits_test'].toSet();

    ProductDetailsResponse response = await InAppPurchaseConnection.instance.queryProductDetails(ids);

    setState(() {
      _products = response.productDetails;
      if(response.productDetails.isNotEmpty){
        print(response.productDetails.elementAt(0));
      } else{
        print("void list");
      }
    });
  }

I also try this page: https://joebirch.co/2019/05/31/adding-in-app-purchases-to-flutter-apps/

My code:

class _MyHomePageState extends State<MyHomePage> {

  //Interface
  InAppPurchaseConnection _iap = InAppPurchaseConnection.instance;

  bool _available = true;
  List<ProductDetails> _products = [];
  List<PurchaseDetails> _purchases = [];
  StreamSubscription _subscription;

  int _credits = 0;


  @override
  void initState() {
    _initialize();
    _getProducts();
    print("Hola");
    super.initState();
  }


  @override
  void dispose() {
    _subscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_available ? "Open for business" : "Not available"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
              Text("Hello World!"),
              Text("$_credits"),
              FlatButton(child: Text("Add Credits"), onPressed: () => _buyProduct(_products.elementAt(0)))
          ],
        ),
      ),
    );
  }


  void _initialize() async {
     _available = await _iap.isAvailable();
     if(_available){
       await _getProducts();
       await _getPastPurchases();

       _verifyPurchase();

       _subscription = _iap.purchaseUpdatedStream.listen((data) => setState((){
         print("New purchase");
         _purchases.addAll(data);
         _verifyPurchase();
       }));
     }
  }

  Future<void> _getProducts() async {

    Set<String> ids = <String>['credits_test'].toSet();

    ProductDetailsResponse response = await InAppPurchaseConnection.instance.queryProductDetails(ids);

    setState(() {
      _products = response.productDetails;
      if(response.productDetails.isNotEmpty){
        print(response.productDetails.elementAt(0));
      } else{
        print("void list");
      }
    });
  }

  void _verifyPurchase(){
    PurchaseDetails purchase = _hasPurchased(testID);
    if(purchase != null && purchase.status == PurchaseStatus.purchased){
      _credits  += 10;
    }
  }

  void _buyProduct(ProductDetails prod){
    final PurchaseParam purchaseParam = PurchaseParam(productDetails: prod);

    _iap.buyConsumable(purchaseParam: purchaseParam, autoConsume: false );
  }
}

My terminal is printing 'void list', and I'm expecting a ProductDetail from the query.

Brewer answered 13/8, 2019 at 16:50 Comment(6)
Most likely products aren't configured properly in the play console.Hashum
if my app in play console is in "Pending Publication" can I make a purchase?Brewer
@rafael Could you solve the problem? I'm having the same issueMcnalley
I think the problem was that the application Id I was running the app with was different to the app registered in the Play StoreMcnalley
did you find the solutionBobettebobina
did you find a fix? still got this errorMood
G
0

have the same problem but a little bit different I have 2 Subscriptions. And if i send only one product id in queryProductDetails it works fine, it dosnt matter what id, but 2 ids returns me an empty result.

Gent answered 24/12, 2023 at 16:33 Comment(0)
P
-3

You need to use a reserved SKU for the test: android.test.purchased

Code: Set<String> ids = <String>['android.test.purchased'].toSet();

Palanquin answered 29/1, 2020 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.