我正在尝试在我的 Flutter 应用程序上进行订阅付款,以便用户可以从中进行选择。
首先我在 Google Play 添加了三个订阅项目。 然后我制作了购买产品的代码。
这是我的代码
class IAPService {
void listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
// ignore: avoid_function_literals_in_foreach_calls
purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
if (kDebugMode) {
print("purchaseDetails.status ${purchaseDetails.status}");
}
if (purchaseDetails.status == PurchaseStatus.purchased ||
purchaseDetails.status == PurchaseStatus.restored) {
_handleSuccessfulPurchase(purchaseDetails);
}
if (purchaseDetails.status == PurchaseStatus.error) {
if (kDebugMode) {
print(purchaseDetails.error!);
}
}
if (purchaseDetails.pendingCompletePurchase) {
await InAppPurchase.instance.completePurchase(purchaseDetails);
if (kDebugMode) {
print("Purchase marked complete");
}
}
});
}
在我的 widger 屏幕上,付款不在 main.dart 上
@override
void initState() {
super.initState();
getuser();
futureData = accountController.fetchAccounts();
final Stream purchaseUpdated = InAppPurchase.instance.purchaseStream;
iapSubscription = purchaseUpdated.listen((purchaseDetailsList) {
if (kDebugMode) {
print("Purchase stream started");
}
IAPService().listenToPurchaseUpdated(purchaseDetailsList);
}, onDone: () {
iapSubscription.cancel();
}, onError: (error) {
iapSubscription.cancel();
}) as StreamSubscription<List<PurchaseDetails>>;
initStoreInfo();
}
但是我的主屏幕上打开的应用程序广告在我返回屏幕后弹出。dart 页面上弹出
class HomeScreenState extends State<HomeScreen>
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
AppOpenManager appOpenManager = AppOpenManager();
bool isPaused = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed) {
isPaused = false;
appOpenManager.showAdIfAvailable();
} else if (state == AppLifecycleState.paused) {
isPaused = true;
}
}
那么如何强制停止该广告我正在使用 Getx 作为状态管理,任何人都可以帮助我
我做到了, 我在加载广告之前添加了检查值
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (userController.getuser!.ads) {
if (state == AppLifecycleState.resumed) {
isPaused = false;
appOpenManager.showAdIfAvailable();
} else if (state == AppLifecycleState.paused) {
isPaused = true;
}
}
}