On this page15 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. Lesson
  5. 1. Why Consent
  6. 2. The Order
  7. 3. The Splash Screen
  8. 4. The Main File
  9. Code Examples
  10. Common Beginner Mistakes
  11. Best Practices
  12. Mini Challenge
  13. Practice Questions
  14. Practice Answers
  15. Key Takeaways

Lecture 003 — Consent & Starting the Ads SDK

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

Before an ad can load, two things must happen in order: ask the user for consent where the law requires it, then initialise the SDK. We do both on the splash screen.

Contents

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


Prerequisites

Lectures 001–002; async/await, Navigator.pushAndRemoveUntil.


Connection With Previous Lectures

We have ids but nothing runs yet. The splash screen is the right place: the user sees a logo while Google decides whether a consent form is needed.


Learning Objectives

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

  • Explain why consent matters (GDPR / UMP)
  • Call requestConsentInfoUpdate and loadAndShowConsentFormIfRequired
  • Initialise MobileAds after consent
  • Navigate on success and on failure

Lesson

Users in the EEA, the UK and some other regions must be asked before personalised ads are used. Google's User Messaging Platform (UMP) shows the form. You design the message in the AdMob dashboard (Privacy & messaging); the plugin shows it.

2. The Order

  1. Ask Google if consent info needs updating (requestConsentInfoUpdate).
  2. Show the form only if required (loadAndShowConsentFormIfRequired).
  3. Initialise the SDK (MobileAds.instance.initialize()).
  4. Go to the dashboard.

If anything fails, still continue to the dashboard — a consent error must never trap the user.

3. The Splash Screen

dart
// lib/screen_splash.dart
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'screen_dashboard.dart';

class ScreenSplash extends StatefulWidget {
  const ScreenSplash({super.key});

  @override
  State<ScreenSplash> createState() => _ScreenSplashState();
}

class _ScreenSplashState extends State<ScreenSplash> {
  @override
  void initState() {
    super.initState();
    _start();
  }

  Future<void> _start() async {
    // Ask for consent first (GDPR / UMP), then start the ads SDK.
    _requestConsent(() async {
      await MobileAds.instance.initialize();
      _goToDashboard();
    });
  }

  void _requestConsent(VoidCallback onDone) {
    ConsentInformation.instance.requestConsentInfoUpdate(
      ConsentRequestParameters(),
      () async {
        // Shows the form only when Google says one is required for this user.
        await ConsentForm.loadAndShowConsentFormIfRequired((formError) {
          onDone();
        });
      },
      (formError) => onDone(),
    );
  }

  void _goToDashboard() {
    if (!mounted) return;
    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (_) => const ScreenDashboard()),
      (route) => false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade900,
      body: const Center(
        child: Text(
          'Flutter AdMob Ads',
          style: TextStyle(color: Colors.white, fontSize: 18),
        ),
      ),
    );
  }
}
  • _requestConsent calls back onDone whether consent succeeded, was not needed, or failed.
  • loadAndShowConsentFormIfRequired shows nothing for users outside regions that need it.
  • if (!mounted) return; — the screen may have been closed while we waited.
  • pushAndRemoveUntil replaces the splash so Back never returns to it.

4. The Main File

dart
// lib/main.dart
import 'package:flutter/material.dart';

import 'screen_splash.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'AdMob Demo',
      theme: ThemeData(colorSchemeSeed: Colors.deepPurple),
      home: const ScreenSplash(),
    );
  }
}

Code Examples

Outside the EEA you will not see the form. To force it in testing, Google's plugin lets you set a debug geography in ConsentRequestParameters with ConsentDebugSettings, using your test device id from the log. Do this only in debug and remove it before release.


Common Beginner Mistakes

1. Initialising before consent — the order matters; consent first.

2. Blocking on a consent error — always call onDone on failure.

3. Forgetting mounted — navigating after the screen is gone throws an error.

4. Not creating a consent message in the AdMob dashboard — the form never appears even in the EEA.


Best Practices

  • Show a splash with branding while consent loads
  • Never block the app on an ad-related failure
  • Initialise the SDK once, early

Mini Challenge

Add a 2-second Future.delayed guard so the splash always shows for at least 2 seconds, even when everything finishes instantly.


Practice Questions

  1. Why do we ask for consent before initialising?
  2. What does if (!mounted) return; protect against?
  3. What should happen when the consent request fails?
  4. Where do you create the consent message?

Practice Answers

  1. Personalised ads need the user's choice first in regulated regions.
  2. Using context after the screen was removed.
  3. Continue to the dashboard.
  4. AdMob dashboard → Privacy & messaging.

Key Takeaways

  • Consent → initialise → navigate
  • Always continue to the app, even on errors
  • The consent message is created in the AdMob dashboard