On this page14 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. Lesson
  5. 1. App Id vs Ad Unit Id
  6. 2. The Manager
  7. 3. Why Not Hard-Code Ids?
  8. Code Examples
  9. Common Beginner Mistakes
  10. Best Practices
  11. Mini Challenge
  12. Practice Questions
  13. Practice Answers
  14. Key Takeaways

Lecture 002 — The AdmobManager: Test vs Real Ids

Category1 — AdMob Basics
Lecture002 of 010
Estimated video length14 min
TargetFlutter 3.44 · Dart 3.12 · google_mobile_ads 9
LevelBeginner
Source codeGitHub — urraan_admob_final

Every ad format needs an ad unit id. Scattering them through the code is how real ids reach a debug build by accident. We keep all ids in one class that automatically hands out test ids while you debug.

Contents

Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways


Prerequisites

Lecture 001: plugin and app ids in place. Dart static, Map, and kDebugMode.


Connection With Previous Lectures

Lecture 001 added the app id (one per app). Now we add the ad unit ids (one per placement and format) in code.


Learning Objectives

By the end of this lecture you will be able to:

  • Explain app id vs ad unit id
  • Write an AdmobManager with test, Android and iOS id maps
  • Switch ids by platform and by debug/release
  • Keep real ids out of debug builds

Lesson

1. App Id vs Ad Unit Id

  • App id (ca-app-pub-…~…, with a tilde) — identifies the whole app. It lives in the manifest and Info.plist.
  • Ad unit id (ca-app-pub-…/…, with a slash) — identifies one placement, such as "the banner on the home screen". It lives in Dart.

2. The Manager

All ids sit in one file. Debug builds return Google's test ids; release builds return your real ids for the platform the app is running on.

dart
// lib/admob/admob_manager.dart
import 'dart:io';

import 'package:flutter/foundation.dart';

class AdmobManager {
  /// In debug builds, show Google's test ads. Never ship real ids while testing.
  static const bool showTestAdsInDebug = true;

  static const _testIds = {
    'bannerAdaptive': 'ca-app-pub-3940256099942544/9214589741',
    'bannerFixed': 'ca-app-pub-3940256099942544/6300978111',
    'interstitial': 'ca-app-pub-3940256099942544/1033173712',
    'rewardedInterstitial': 'ca-app-pub-3940256099942544/5354046379',
    'rewarded': 'ca-app-pub-3940256099942544/5224354917',
    'native': 'ca-app-pub-3940256099942544/2247696110',
    'appOpen': 'ca-app-pub-3940256099942544/9257395921',
  };

  // Your real ids from the AdMob dashboard (Apps → Ad units).
  static const _androidIds = {
    'bannerAdaptive': '',
    'bannerFixed': '',
    'interstitial': '',
    'rewardedInterstitial': '',
    'rewarded': '',
    'native': '',
    'appOpen': '',
  };

  static const _iosIds = {
    'bannerAdaptive': '',
    'bannerFixed': '',
    'interstitial': '',
    'rewardedInterstitial': '',
    'rewarded': '',
    'native': '',
    'appOpen': '',
  };

  static Map<String, String> get _ids {
    if (kDebugMode && showTestAdsInDebug) return _testIds;
    return Platform.isAndroid ? _androidIds : _iosIds;
  }

  static String get bannerAdaptiveId => _ids['bannerAdaptive'] ?? '';
  static String get bannerFixedId => _ids['bannerFixed'] ?? '';
  static String get interstitialId => _ids['interstitial'] ?? '';
  static String get rewardedInterstitialId =>
      _ids['rewardedInterstitial'] ?? '';
  static String get rewardedId => _ids['rewarded'] ?? '';
  static String get nativeId => _ids['native'] ?? '';
  static String get appOpenId => _ids['appOpen'] ?? '';
}
  • kDebugMode — true only when running in debug.
  • showTestAdsInDebug — a switch to turn real ads on in debug later (for a final check).
  • The real-id maps are empty now. You fill them in Lecture 010 from the AdMob dashboard.
  • Each getter (bannerAdaptiveId, …) hides the map keys, so the rest of the app writes AdmobManager.interstitialId and never a raw string.

3. Why Not Hard-Code Ids?

A hard-coded real id in a debug build means your own testing taps produce invalid traffic. One class with a debug switch makes that mistake impossible.


Code Examples

Example 1 — Using an Id

dart
adUnitId: AdmobManager.interstitialId,

In debug this is ca-app-pub-3940256099942544/1033173712, Google's sample interstitial.


Common Beginner Mistakes

1. An empty id in release — ads fail to load with an invalid ad unit error. Fill every id before release.

2. Android id in the iOS map — ads do not load on iOS.

3. Real id + debug — only if you set showTestAdsInDebug = false on purpose.


Best Practices

  • One id per placement and format
  • Keep ids in one file; never in widgets
  • Create separate ad units for separate screens so reports show what earns

Mini Challenge

Add an 'appOpen' entry check: print AdmobManager.appOpenId from main in debug and confirm it is Google's sample id.


Practice Questions

  1. What is the difference in format between an app id and an ad unit id?
  2. What does kDebugMode do here?
  3. Why are there two real-id maps?
  4. Where do real ids come from?

Practice Answers

  1. App id has ~, ad unit id has /.
  2. Makes debug builds return test ids.
  3. Android and iOS have different ad unit ids.
  4. The AdMob dashboard, under Apps → Ad units.

Key Takeaways

  • App id in platform files, ad unit ids in AdmobManager
  • Debug = test ids, release = real ids
  • Fill the real maps before release