你好,我在官方 flutter 插件 in_app_purchase: ^3.0.7 的帮助下,在我的应用程序中使用 in purchase 购买非消耗品。但问题是,当我第一次在用户购买时启动我的应用内购买屏幕。它不会第一次初始化 google play 对话以进行付款,但如果用户返回 sacreen 然后来到这个 sacreen 它会正常工作。谁能告诉我为什么它不能在第一次初始化时工作。下面是我的代码。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Purchasenow extends StatefulWidget {
@override
State<Purchasenow> createState() => _PurchasenowState();
}
class _PurchasenowState extends State<Purchasenow> {
final Set<String> _productIds = {'urdubible'};
late List<ProductDetails> _products;
@override
void initState() {
super.initState();
_loadProducts();
_checkPurchaseStatus();
// initialize the in-app purchase plugin
InAppPurchase .instance.purchaseStream
.listen((List<PurchaseDetails> purchases) {
// handle any purchase updates
_handlePurchaseUpdates(purchases);
});
// load the available products
}
Future<void> _loadProducts() async {
// get the product details from the store
ProductDetailsResponse response =
await InAppPurchase.instance.queryProductDetails(_productIds);
// save the product details to state
setState(() {
_products = response.productDetails;
});
}
void _handlePurchaseUpdates(List<PurchaseDetails> purchases) {
for (PurchaseDetails purchase in purchases) {
switch (purchase.status) {
case PurchaseStatus.purchased:
_savePurchaseStatus(true);
// handle the purchase, e.g. update UI, save the purchase to database, etc.
setState(() {
});
break;
case PurchaseStatus.error:
// handle any purchase errors
break;
case PurchaseStatus.canceled:
// handle cancelled purchases
break;
}
}
}
Future<void> _purchaseProduct() async {
if (_products.isNotEmpty) {
// initiate the purchase flow
final PurchaseParam purchaseParam = PurchaseParam(productDetails: _products.first);
InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
}
}
Future<void> _savePurchaseStatus(bool isPurchased) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('isPurchased', isPurchased);
}
Future<void> _checkPurchaseStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool? isPurchased = prefs.getBool('isPurchased');
if (isPurchased != null && isPurchased) {
setState(() {
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
elevation: 0.0,
title: Text('ADS FREE'),
),
body: Padding(
padding: EdgeInsets.all(10),
child: Column(
children: [
Text(
"بٹن پر کلک کریں اور اس ایپ کے تاحیات استعمال کے لیے ادائیگی کریں.۔",style: GoogleFonts.notoNastaliqUrdu(
fontSize: 18
),
textDirection: TextDirection.rtl,
),
SizedBox(height: 10,),
Text(
"Click on button and make the payment for life time usage of this app.",style: TextStyle(
fontSize: 22
),
),
SizedBox(height: 45,),
Center(
child: Center(child: SizedBox(
width: 200.0,
height: 40,
child:ElevatedButton(
style: ElevatedButton.styleFrom(),
onPressed: _purchaseProduct,
child: const Text('Purchase Now!')
),
)),
),
],
),
),
);
}
}