简体   繁体   English

插页式广告仅展示一次 - Flutter/Dart

[英]Interstitial ad showing only for once - Flutter/Dart

I'm trying to show interstitial ad on every 3rd click (basically after certain number of clicks) On first 3rd click, it shows interstitial ad perfectly but after that it throws the following error我试图在每 3 次点击时显示插页式广告(基本上在一定次数的点击后)在第 3 次点击时,它完美地显示插页式广告,但之后它会引发以下错误

ad has not been loaded or has already been disposed.广告尚未加载或已被处置。



  bool _isLoaded = false;
  InterstitialAd? interstitialAd;
  int counter = 0;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    showInterstitialAd();
  }

  void showInterstitialAd(){
    InterstitialAd.load(
        adUnitId: "ad_id",
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
            onAdLoaded: (ad) {
              _isLoaded = true;
              interstitialAd=ad;

              interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
                  onAdDismissedFullScreenContent: (InterstitialAd ad){
                    print('ad dismissed');
                    interstitialAd!.dispose();
                  },
                  onAdFailedToShowFullScreenContent: (InterstitialAd ad, adError){
                    print('ad failed');
                    interstitialAd!.dispose();
                  }
              );
            },
            onAdFailedToLoad: (error) {
              interstitialAd!.dispose();
            }
        ));
  }


// show ad when counter is equal to 3
void onTap (){
           if(_isLoaded && counter == 3){
                interstitialAd!.show();
                counter = 0;
           } else {
                counter ++;
           }
       }

Please rename your showInterstitialAd to loadInterstitialAd (because method only load and prepare ads) and call it after counter = 0;请将您的showInterstitialAd重命名为loadInterstitialAd (因为方法只加载和准备广告)并在counter = 0;

Should look like this:应该是这样的:

if(_isLoaded && counter == 3){
      interstitialAd!.show();
      counter = 0;
      loadInterstitialAd();
    } else {
        counter ++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM