0

I'm using the plugin: firebase_admob: ^0.9.3+4 And I want to load ads only on a specific page in a PageView. When the user moves to another page I want the ads to be disposed. However, I've encountered a problem where the ad only gets disposed if its already loaded and shown. Otherwise it loads and displays on the new tab.

How can I prevent an ad from loading if it hasn't loaded already?

Here's the code:

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
  keywords: <String>['flutterio', 'beautiful apps'],
  contentUrl: 'https://flutter.io',
  birthday: DateTime.now(),
  childDirected: false,
  designedForFamilies: false,
  gender: MobileAdGender.male, 
  testDevices: <String>[], 
);

BannerAd myBanner = BannerAd(
  adUnitId: BannerAd.testAdUnitId,
  size: AdSize.smartBanner,
  targetingInfo: targetingInfo,
  listener: (MobileAdEvent event) {
    print("BannerAd event is $event");
  },
);
myBanner
  ..load()
  ..show(
    anchorOffset: 60.0,
    horizontalCenterOffset: 10.0,
    anchorType: AnchorType.bottom,
  );

And in the dispose method of the page I call the following function:

@override
  void dispose() {
    disposeAds();
    super.dispose();
}

void disposeAds()async{
    _banner?.dispose();
    _banner=null;
}

What I want to know is how to prevent an ad from loading after the page is disposed? On iOS it automatically seems to do this but not on Android.

123432198765
  • 276
  • 3
  • 14

1 Answers1

0

You can use admob_flutter

This package transform your Admob banner in a widget

AdmobBanner(
      adUnitId: AdManager.bannerAdUnitId,
      adSize: AdmobBannerSize.BANNER,
    ));

then, you can use like any other widget

 Container getBanner() {
    return new Container(
        decoration: BoxDecoration(border: Border.all(color: Colors.black)),
        child: AdmobBanner(
          adUnitId: AdManager.bannerAdUnitId,
          adSize: AdmobBannerSize.BANNER,
        ));
  }