kmcgill88 / admob_flutter

Admob Flutter plugin that shows banner ads using native platform views.
https://pub.dartlang.org/packages/admob_flutter
ISC License
435 stars 151 forks source link

Ability to preload banner ads #232

Open chris-rutkowski opened 3 years ago

chris-rutkowski commented 3 years ago

Hello, even in your example in README, a banner is in a Scrollable View. I would like to request a feature to preload banner before showing it.

Ideally, my implementation would look like this:

MyUtilityClass.dart

class MyUtilityClass {
  AdmobBannerAd bannerAd;
  bool isBannerAdReady = false;

  void _loadBannerAd() {
    isBannerAdReady = false;
    final cleanup = () {
      bannerAd.dispose();
      bannerAd = null;
    };

    bannerAd = AdmobBannerAd(
      adUnitId: myBannerAdID,
      adSize: AdmobBannerSize.MEDIUM_RECTANGLE,
      listener: (event, _) async {
        if (event == AdmobAdEvent.failedToLoad) {
          cleanup();
          await Future.delayed(Duration(seconds: 1));
          _loadBannerAd();
        } else if (event == AdmobAdEvent.closed) {
          cleanup();
          _loadBannerAd();
        } else if (event == AdmobAdEvent.loaded) {
          isBannerAdReady = true;
        } else {
          ... some other logic
        }
      },
    );
    bannerAd.load();
  }
}

and in some class responsible for generating ListView content I check if isBannerAdReady == true and display like this:

      AdmobBanner(
        ad: Provider.of<MyUtilityClass>(context, listen: false).bannerAd,
      )

This gives us several benefits:

I would appreciate if you check my app 📲 https://well-spoken.app . After completing the tutorial, visit Learn mode again, if you scroll a bit, you will often see empty white boxes that turn into banners after a while. If I have already preloaded instance this glitch would be fixed.

Appreciate help, and keep up good work :)

it-one-mm commented 3 years ago

Don't use admob_flutter. Just use firebase_admob, and it is reliable.