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.
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. – Freemasonrysuper.dispose();
when just havingmyBanner?.dispose();
created that error and the problem persisted after I added thesuper.dispose()
. Is there some kind of hidden varible that deals with admob? – DevolvemyBanner?.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. – Freemasonryvoid initState() { super.initState(); myBanner ..load() ..show(anchorOffset: 10.0, anchorType: AnchorType.bottom); }
– Devolve