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.