On this page15 sections
Lecture 003 — Consent & Starting the Ads SDK
| Category | 1 — AdMob Basics |
| Lecture | 003 of 010 |
| Estimated video length | 18 min |
| Target | Flutter 3.44 · Dart 3.12 · google_mobile_ads 9 |
| Level | Beginner |
| Source code | GitHub — 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
requestConsentInfoUpdateandloadAndShowConsentFormIfRequired - Initialise
MobileAdsafter consent - Navigate on success and on failure
Lesson
1. Why Consent
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
- Ask Google if consent info needs updating (
requestConsentInfoUpdate). - Show the form only if required (
loadAndShowConsentFormIfRequired). - Initialise the SDK (
MobileAds.instance.initialize()). - Go to the dashboard.
If anything fails, still continue to the dashboard — a consent error must never trap the user.
3. The Splash Screen
// 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),
),
),
);
}
}_requestConsentcalls backonDonewhether consent succeeded, was not needed, or failed.loadAndShowConsentFormIfRequiredshows nothing for users outside regions that need it.if (!mounted) return;— the screen may have been closed while we waited.pushAndRemoveUntilreplaces the splash so Back never returns to it.
4. The Main File
// 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
Example 1 — Seeing the Consent Form While Testing
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
- Why do we ask for consent before initialising?
- What does
if (!mounted) return;protect against? - What should happen when the consent request fails?
- Where do you create the consent message?
Practice Answers
- Personalised ads need the user's choice first in regulated regions.
- Using
contextafter the screen was removed. - Continue to the dashboard.
- 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