我正在观看此视频:https://www.youtube.com/watch?v=NWbkKH-2xcQ&t=495s,我正在使用Android设备。
[当我尝试从Google Play控制台获取产品时,我在此部分中得到一个空白列表。
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");
}
});
}
我也尝试此页面:https://joebirch.co/2019/05/31/adding-in-app-purchases-to-flutter-apps/
我的代码:
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 );
}
}
我的终端正在打印“无效列表”,并且我希望从查询中获得一个ProductDetail。
您需要使用保留的SKU进行测试:android.test.purchased
代码:Set<String> ids = <String>['android.test.purchased'].toSet();