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

3

6

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.

Devolve answered 19/7, 2019 at 2:49 Comment(6)
you can just dispose the banner with myBanner?.dispose(); and remove it from the dispose method. Or leave it in dispose method and remove from button click. I think it's being called twice, on button click and on widget disposal.Freemasonry
So I added the extra super.dispose(); when just having myBanner?.dispose(); created that error and the problem persisted after I added the super.dispose(). Is there some kind of hidden varible that deals with admob?Devolve
Actually I mean to say, you have myBanner?.dispose() called twice may be, once on button click and next on dispose method making it to try dispose again and hence the error cannot be null as it was disposed already.Freemasonry
To solve this with using dispose, do I change the way my banner loads so it will load a new banner on a pop? Something with this?void initState() { super.initState(); myBanner ..load() ..show(anchorOffset: 10.0, anchorType: AnchorType.bottom); } Devolve
Same issue, did you fixed it?Lightfooted
Sorry for the late reply. I did. But not by actually figuring out what was wrong with my code. I ended up using a different set up competely. I switched to using the ads package. Makes controlling ads much easier. Its here: pub.dev/packages/ads. I am using version 0.5.1 i did this a while ago and dont exactly remember why i wasnt using the most up to date version. Something about compatible with other packages.Devolve
R
3

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");
    }
  }
Rabia answered 5/5, 2020 at 11:23 Comment(0)
R
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.

Reiners answered 29/10, 2019 at 12:48 Comment(0)
E
2

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) {}
Edroi answered 6/3, 2020 at 22:40 Comment(2)
Same here. However using try/catch did not hide the banner. Shame on this flutter plugin. No way to hide it.Geostatics
I've found out. You have to call _bannerAd= null after _bannerAd?.dispose() for it to close.Geostatics

© 2022 - 2024 — McMap. All rights reserved.