How do I fix "Failed assertion: line 227 pos 12: '_allAds[id] !=null': is not true."?

2,993

Solution 1

As of firebase_admob "v0.9.3+2" this code works for me

void disposeAd() {
    log.d("Calling disposeAd");
    try {

      _bannerAd?.dispose();
      _bannerAd = null;

    } catch (ex) {
      log.e("banner dispose error");
    }
  }

Solution 2

I fixed this issue by commenting //assert(_allAds[id] != null); in firebase_admob.dart file. I'm not sure if this is the best solution but it worked fine for me.

Solution 3

This assertion in the library is just annoying.
They haven't provided any means of hiding a banner ad other than dispose it, and I often got bitten by this assertion...

My workaround is (sadly):

try {
    _bannerAd?.dispose();
  } catch (ex) {}
Share:
2,993
David H.
Author by

David H.

Updated on December 12, 2022

Comments

  • David H.
    David H. over 1 year

    I am trying to add banner ads to my app. I want the banner to display on certain pages of my app but once you get to certain pages, I want the banner to go away. So I am getting my banner to show up and it is working. I also found using

    super.dispose();
    myBanner?.dispose();
    

    on my buttons on the onPressed will get rid of the banner. When I pop back to the page that loads the banner, though, I get an error:

    I/flutter ( 7058): The following assertion was thrown while handling a gesture: I/flutter ( 7058): 'package:firebase_admob/firebase_admob.dart': Failed assertion: line 227 pos 12: '_allAds[id] != I/flutter ( 7058): null': is not true.

    Then I can not click on any buttons that have my dispose on the onPressed.

    I've tried adding the super.dispose() as I only had the myBanner?.dispose(); before, but the result was the same. I am having trouble finding very much info on admobs.

    Here is how I set up my banner:

        BannerAd myBanner = BannerAd(
          // Replace the testAdUnitId with an ad unit id from the AdMob dash.
          // https://developers.google.com/admob/android/test-ads
          // https://developers.google.com/admob/ios/test-ads
          adUnitId: adUnitId,
          size: AdSize.smartBanner,
          targetingInfo: targetingInfo,
          listener: (MobileAdEvent event) {
            print("BannerAd event is $event");
          },
        );
    
    

    Then in my class:

        class EnMainMenu extends StatefulWidget {
          @override
          State<StatefulWidget> createState() => _MainMenuState();
        }
        
        class _MainMenuState extends State<EnMainMenu> {
          @override
          void initState() {
            super.initState();
        
            myBanner
              ..load()
              ..show(anchorOffset: 10.0, anchorType: AnchorType.bottom);
          }
    
    

    and how I'm using my buttons:

     Align(
                    alignment: Alignment.topRight,
                    child: Container(
                        padding: EdgeInsets.all(20),
                        child: IconButton(
                          icon: Icon(
                            Icons.settings,
                            color: Colors.black45,
                            size: 40,
                          ),
                          onPressed: () {
                            myBanner?.dispose();
                            super.dispose();
                            Navigator.of(context).pushNamed('/EnSettings');
                          },
                        ))),
    

    If there is a better way to get the banner to show and hide on certain pages, please let me know. I am pretty new to flutter and dart. In the future, I would also be looking to add interstitial ads as well.