Search code examples
androidiosflutteradmobids

AdMob banner : different ID on iOS and Android


I'm working on a mobile app under Flutter, and I added an ad block on my app via AdMob.

I have created 2 'apps' on the AdMob website, one Android and one iOS. So I get a different ID for each of my elements.

The 2 applications I created on AdMob

In my code, I did it like this, is it the right solution?

final BannerAd myBanner = BannerAd(
    adUnitId: (Platform.isAndroid) ? 'ca-app-pub-xxxxxxxxxxxxxxxxx/yyyyyyyyyy' : 'ca-app-pub-zzzzzzzzzzzzzzzzz/aaaaaaaaaa',
    size: AdSize.banner,
    request: const AdRequest(),
    listener: const BannerAdListener(),
  );

Here, of course, the a, x, y and z replace the real values, but this is to show that I put different IDs for the platforms.

Thank you !


Solution

  • You can create a class like adhelper.dart and add your Keys there. Checkout below example.

    import 'dart:io';
    
    class AdHelper {
      static String get bannerAdUnitId {
        if (Platform.isAndroid) {
          return 'ca-app-pub-2955168626873489/4257713437';
        } else if (Platform.isIOS) {
          return 'ca-app-pub-3940256099942544/2934735716';
        }
        throw UnsupportedError("Unsupported platform");
      }
    
      static String get insAdUnitId {
        if (Platform.isAndroid) {
          return 'ca-app-pub-2955168626873489/9416285743';
        } else if (Platform.isIOS) {
          return 'ca-app-pub-3940256099942544/3986624511';
        }
        throw UnsupportedError("Unsupported platform");
      }
    }
    

    Now If you want to use it in your BannerAds then you can use like below

    adUnitId: AdHelper.bannerAdUnitId,