Adding AdMob Ads to a Flutter App: A Complete Guide

Monetizing your mobile app with Google AdMob is a popular choice among Flutter developers. AdMob provides an easy way to integrate various ad formats like banner ads, interstitial ads, and rewarded ads, which can help generate revenue for your Flutter application.

In this guide, we’ll walk through how to set up AdMob in Flutter and implement these three ad types. By the end, you’ll have a fully functioning Flutter app with integrated ads that can help you start monetizing your app right away.

Adding AdMob ads to a Flutter app

Prerequisites

Step 1: Setting Up AdMob

1.1. Register Your App in AdMob

  1. Go to AdMob and sign in.
  2. Click on Apps > Add app.
  3. Select Add app manually and follow the prompts to create your app.
  4. Note down the App ID as you will need this later.

1.2. Create Ad Units

  1. After creating your app, go to Apps > Your App > Ad units.
  2. Create the ad units for each ad type you plan to use:
    • Banner Ad
    • Interstitial Ad
    • Rewarded Ad
  3. Note down the Ad Unit IDs for each ad type.

Step 2: Add AdMob to Flutter

2.1. Add google_mobile_ads Package

In your pubspec.yaml file, add the google_mobile_ads package, which is the official plugin for AdMob in Flutter.

dependencies:
flutter:
sdk: flutter
google_mobile_ads: ^2.0.1

After adding the dependency, run:

flutter pub get

2.2. Update AndroidManifest.xml and Info.plist

For Android:

  1. Open the AndroidManifest.xml file located at android/app/src/main/AndroidManifest.xml.
  2. Inside the <application> tag, add your AdMob App ID:
    • <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="YOUR_ADMOB_APP_ID" />

For iOS:

  1. Open the Info.plist file located at ios/Runner/Info.plist.
  2. Add your AdMob App ID:
    • <key>GADApplicationIdentifier</key> <string>YOUR_ADMOB_APP_ID</string>

Step 3: Initialize AdMob in Flutter

In your main Dart file (e.g., main.dart), initialize the GoogleMobileAds SDK before running the app.

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

void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MyApp());
}

Step 4: Implement Banner, Interstitial, and Rewarded Ads

4.1. Banner Ads

Banner ads are simple, static ads that appear at the top or bottom of the screen.

  1. Create a BannerAd instance.
BannerAd bannerAd = BannerAd(
  adUnitId: 'YOUR_BANNER_AD_UNIT_ID',
  request: AdRequest(),
  size: AdSize.banner,
  listener: BannerAdListener(
    onAdLoaded: (_) => print('Banner Ad Loaded'),
    onAdFailedToLoad: (ad, error) {
      ad.dispose();
      print('Banner Ad Failed to Load: $error');
    },
  ),
)..load();

2. Display the banner ad using AdWidget.

Container(
  alignment: Alignment.bottomCenter,
  child: AdWidget(ad: bannerAd),
  width: bannerAd.size.width.toDouble(),
  height: bannerAd.size.height.toDouble(),
)

4.2. Interstitial Ads

Interstitial ads are full-screen ads that display at natural breaks in the app, such as between game levels.

  1. Load an InterstitialAd when the app starts.
InterstitialAd? interstitialAd;

void loadInterstitialAd() {
  InterstitialAd.load(
    adUnitId: 'YOUR_INTERSTITIAL_AD_UNIT_ID',
    request: AdRequest(),
    adLoadCallback: InterstitialAdLoadCallback(
      onAdLoaded: (ad) => interstitialAd = ad,
      onAdFailedToLoad: (error) => print('Interstitial Failed to Load: $error'),
    ),
  );
}

2. Display the interstitial ad at appropriate moments.

void showInterstitialAd() {
  if (interstitialAd != null) {
    interstitialAd!.show();
    interstitialAd = null; // Dispose after showing
    loadInterstitialAd(); // Load a new ad for future use
  }
}

4.3. Rewarded Ads

Rewarded ads offer users incentives for watching, like in-game currency or points.

  1. Load a RewardedAd when the app starts.
RewardedAd? rewardedAd;

void loadRewardedAd() {
  RewardedAd.load(
    adUnitId: 'YOUR_REWARDED_AD_UNIT_ID',
    request: AdRequest(),
    rewardedAdLoadCallback: RewardedAdLoadCallback(
      onAdLoaded: (ad) => rewardedAd = ad,
      onAdFailedToLoad: (error) => print('Rewarded Ad Failed to Load: $error'),
    ),
  );
}

2. Show the rewarded ad and provide a callback for when the user earns a reward.

void showRewardedAd() {
  if (rewardedAd != null) {
    rewardedAd!.show(
      onUserEarnedReward: (ad, reward) {
        print('Reward earned: ${reward.amount} ${reward.type}');
      },
    );
    rewardedAd = null; // Dispose after showing
    loadRewardedAd(); // Load a new ad for future use
  }
}

Best Practices and Tips

  1. Test with Test Ads: Use test ads during development to avoid accidental invalid clicks on real ads. Replace the adUnitId with BannerAd.testAdUnitId, InterstitialAd.testAdUnitId, or RewardedAd.testAdUnitId as needed.
  2. Manage Ad Load and Display: Load ads in advance and only display them at natural breaks to improve the user experience.
  3. Handle Failures Gracefully: Ad loading can fail due to network issues or other reasons. Use callbacks to handle loading errors gracefully and ensure ads are reloaded.

Conclusion

Integrating AdMob into a Flutter app can significantly enhance your app’s monetization potential. By following this guide, you’ve learned how to add banner ads, interstitial ads, and rewarded ads to your app, allowing you to provide a valuable experience to users while generating revenue. Make sure to follow best practices, and don’t forget to analyze your ad performance in the AdMob dashboard to optimize your ad strategy!

Explore Other Flutter Topics…

  1. Introduction to Flutter and Dart
  2. Why choose Flutter
  3. Installing Flutter On Your Windows Mac And Linux System
  4. Your first Flutter app
  5. Flutter project structure
  6. Building blocks of Flutter
  7. Stateful vs. Stateless Widgets Explained
  8. Flutter layout system
  9. Flutter text widget
  10. Creating Buttons in Flutter: ElevatedButton, TextButton, and IconButton
  11. Handling User Input with Flutter Forms
  12. Container class in Flutter
  13. Flutter Navigation
  14. Flutter – Pass Data One Screen To Another Screen
  15. Managing Device Orientation in Flutter
  16. Stateful widget lifecycle in Flutter
  17. Future of Flutter
  18. Flutter Themes
  19. Flutter Animations
  20. Flutter AppBar Customization
  21. ListView in Flutter
  22. Flutter GridView
  23. Flutter Expanded Widget
  24. Flutter BottomNavigation Bar
  25. Floating Action Button
  26. Drawer Widgets in Flutter
  27. Form Validation in Flutter
  28. Flutter TextField
  29. Adding AdMob ads to a Flutter app
  30. Building Flutter Web & Desktop Applications
  31. What is Async and Await in Flutter
  32. HTTP requests in Flutter
  33. Parsing JSON in Flutter
  34. Tinder-Style Swipe Cards in Flutter
  35. Flutter Tic Tac Toe Game Tutorial
  36. Flutter Login UI Tutorial
  37. Flutter Card Widget Tutorial
  38. Flutter music player app tutorial
  39. Flutter introduction screens
  40. Shared Preferences in Flutter
  41. SQLite Database in Flutter
  42. Firebase Authentication in Flutter
  43. Firebase Firestore in Flutter
  44. Push Notifications in Flutter
  45. Handling File Uploads in Flutter
  46. Responsive Design in Flutter
  47. Provider in Flutter
  48. Riverpod in Flutter
  49. Flutter BLoC Pattern Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *