What must you call before using AdMob ads?
MobileAds.instance.initialize() must be called in main() before showing any ads.
Monetize your app with ads and one-time purchases
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Make money from your app with Google AdMob for ads and In-App Purchases for one-time or recurring payments. These are the two most common monetization strategies for mobile apps, and understanding both options allows you to choose the right revenue model for your audience and app category.
1// ── ADMOB ──2// pubspec.yaml: google_mobile_ads: ^4.0.03import 'package:google_mobile_ads/google_mobile_ads.dart';45// Initialize in main6void main() async {7WidgetsFlutterBinding.ensureInitialized();8await MobileAds.instance.initialize();9runApp(const MyApp());10}1112// ── Banner Ad ──13class BannerAdWidget extends StatefulWidget { ... }14class _BannerAdWidgetState extends State<BannerAdWidget> {15BannerAd? _bannerAd;1617@override18void initState() {19super.initState();20_bannerAd = BannerAd(21adUnitId: 'ca-app-pub-xxxxx/xxxxx', // your ad unit ID22size: AdSize.banner,23request: const AdRequest(),24listener: BannerAdListener(25onAdLoaded: (_) => setState(() {}),26onAdFailedToLoad: (ad, error) => ad.dispose(),27),28)..load();29}3031@override32Widget build(BuildContext context) {33if (_bannerAd == null) return const SizedBox.shrink();34return SizedBox(35height: _bannerAd!.size.height.toDouble(),36child: AdWidget(ad: _bannerAd!),37);38}3940@override41void dispose() {42_bannerAd?.dispose();43super.dispose();44}45}4647// ── Interstitial Ad ──48InterstitialAd? _interstitialAd;4950void loadInterstitial() {51InterstitialAd.load(52adUnitId: 'ca-app-pub-xxxxx/xxxxx',53request: const AdRequest(),54adLoadCallback: InterstitialAdLoadCallback(55onAdLoaded: (ad) => _interstitialAd = ad,56onAdFailedToLoad: (error) => print('Failed: $error'),57),58);59}6061void showInterstitial() {62_interstitialAd?.show();63_interstitialAd = null;64loadInterstitial(); // preload next65}
1// ── IN-APP PURCHASES ──2// pubspec.yaml: in_app_purchase: ^3.1.03import 'package:in_app_purchase/in_app_purchase.dart';45final iap = InAppPurchase.instance;67// Check availability8final available = await iap.isAvailable();910// Get products11const productIds = {'premium_monthly', 'remove_ads'};12final response = await iap.queryProductDetails(productIds);13final products = response.productDetails;1415// Buy a product16iap.buyNonConsumable(purchaseParam: PurchaseParam(productDetails: product));1718// Listen for purchase updates19iap.purchaseStream.listen((purchases) {20for (final purchase in purchases) {21if (purchase.status == PurchaseStatus.purchased) {22// ✅ Unlock feature, verify receipt on server23await iap.completePurchase(purchase);24} else if (purchase.status == PurchaseStatus.error) {25// ❌ Handle error26}27}28});
Never use real ad unit IDs during development. Use the official Google test IDs instead. - Banner test ID: ca-app-pub-3940256099942544/6300978111 - Interstitial test ID: ca-app-pub-3940256099942544/1033173712 Using real ads in test builds can get your AdMob account banned!
Ads and purchases should complement each other rather than compete:
Balanced monetization improves revenue without sacrificing long-term engagement.
Monetize your app with ads and one time purchases. Monetization features help apps earn revenue while preserving user trust and value.
Operate with production and interview-level clarity.
Show value first, then present clear and honest upgrade options.
Bad monetization UX increases churn even when app features are strong.
Monetization code should stay modular to avoid coupling revenue logic with core UI flows.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
MobileAds.instance.initialize() must be called in main() before showing any ads.
Consumable (coins, gems) can be bought repeatedly. Non-consumable (remove ads, premium) is a one-time purchase.
completePurchase() acknowledges to Google Play/App Store that the purchase was delivered. Missing this can cause refunds.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue