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.
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");
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With