Skip to content
This repository was archived by the owner on Mar 12, 2022. It is now read-only.

Initialize

Bruno D'Luka edited this page Jan 18, 2021 · 21 revisions

Initialize

Before creating any ads, you must initalize the admob. It can be initialized only once:

import 'package:flutter/foundation.dart';

String get nativeAdUnitId {
  /// Always test with test ads
  if (kDebugMode)
    return MobileAds.nativeAdTestUnitId;
  else return 'your-native-ad-unit-id';
}

String get bannerAdUnitId {
  /// Always test with test ads
  if (kDebugmode)
    return MobileAds.bannerAdTestUnitId;
  else return 'you-banner-ad-unit-id';
}

String get interstitialAdUnitId {
  /// Always test with test ads
  if (kDebugmode)
    return MobileAds.interstitialAdTestUnitId;
  else return 'you-interstitial-ad-unit-id';
}

void main() {
  // Add this line if you will initialize it before runApp
  WidgetsFlutterBinding.ensureInitialized();
  MobileAds.initialize(
    nativeAdUnitId: nativeAdUnitId,
    bannerAdUnitId: bannerAdUnitId,
    interstitialAdUnitId: interstitialAdUnitId,
  );
  runApp(MyApp());
}

❗NOTE:❗ Unit IDs are NOT App IDs

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

Test Ids:

They've been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK's test ads work, see Test Ads.

Clone this wiki locally