Don't miss ourDecember livestream, with updates on Flutter and live Q&A!
Add ads to your mobile Flutter app or game
How to use the google_mobile_ads package to show ads in Flutter.
Many developers use advertising to monetize their mobile apps and games. This allows their app to be downloaded free of charge, which improves the app's popularity.

To add ads to your Flutter project, useAdMob, Google's mobile advertising platform. This recipe demonstrates how to use thegoogle_mobile_ads package to add a banner ad to your app or game.
Apart from AdMob, thegoogle_mobile_ads package also supports Ad Manager, a platform intended for large publishers. Integrating Ad Manager resembles integrating AdMob, but it won't be covered in this cookbook recipe. To use Ad Manager, follow theAd Manager documentation.
1. Get AdMob App IDs
#Go toAdMob and set up an account. This could take some time because you need to provide banking information, sign contracts, and so on.
With the AdMob account ready, create twoApps in AdMob: one for Android and one for iOS.
Open theApp settings section.
Get the AdMobApp IDs for both the Android app and the iOS app. They resemble
ca-app-pub-1234567890123456~1234567890. Note the tilde (~) between the two numbers.
2. Platform-specific setup
#Update your Android and iOS configurations to include your App IDs.
Android
#Add your AdMob app ID to your Android app.
Open the app's
android/app/src/main/AndroidManifest.xmlfile.Add a new
<meta-data>tag.Set the
android:nameelement with a value ofcom.google.android.gms.ads.APPLICATION_ID.Set the
android:valueelement with the value to your own AdMob app ID that you got in the previous step. Include them in quotes as shown:xml<manifest><application>...<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713--><meta-dataandroid:name="com.google.android.gms.ads.APPLICATION_ID"android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/></application></manifest>
iOS
#Add your AdMob app ID to your iOS app.
Open your app's
ios/Runner/Info.plistfile.Enclose
GADApplicationIdentifierwith akeytag.Enclose your AdMob app ID with a
stringtag. You created this AdMob App ID instep 1.xml<key>GADApplicationIdentifier</key><string>ca-app-pub-################~##########</string>
3. Add thegoogle_mobile_ads plugin
# To add thegoogle_mobile_ads plugin as a dependency, runflutter pub add:
flutter pub add google_mobile_ads Once you add the plugin, your Android app might fail to build with aDexArchiveMergerException:
Error while merging dex archives:The number of method references in a .dex file cannot exceed 64K. To resolve this, execute theflutter run command in the terminal, not through an IDE plugin. Theflutter tool can detect the issue and ask whether it should try to solve it. Answery, and the problem goes away. You can return to running your app from an IDE after that.

4. Initialize the Mobile Ads SDK
#You need to initialize the Mobile Ads SDK before loading ads.
Call
MobileAds.instance.initialize()to initialize the Mobile Ads SDK.dartvoidmain()async{WidgetsFlutterBinding.ensureInitialized();unawaited(MobileAds.instance.initialize());runApp(constMyApp());}
Run the initialization step at startup, as shown above, so that the AdMob SDK has enough time to initialize before it is needed.
MobileAds.instance.initialize() returns aFuture but, the way the SDK is built, you don't need toawait it. If you try to load an ad before thatFuture is completed, the SDK will gracefully wait until the initialization, andthen load the ad. You can await theFuture if you want to know the exact time when the AdMob SDK is ready.
5. Load a banner ad
#To show an ad, you need to request it from AdMob.
To load a banner ad, construct aBannerAd instance, and callload() on it.
The following code snippet refers to fields such aadSize,adUnitId and_bannerAd. This will all make more sense in a later step.
/// Loads a banner ad.void_loadAd(){finalbannerAd=BannerAd(size:widget.adSize,adUnitId:widget.adUnitId,request:constAdRequest(),listener:BannerAdListener(// Called when an ad is successfully received.onAdLoaded:(ad){if(!mounted){ad.dispose();return;}setState((){_bannerAd=adasBannerAd;});},// Called when an ad request failed.onAdFailedToLoad:(ad,error){debugPrint('BannerAd failed to load:$error');ad.dispose();},),);// Start loading.bannerAd.load();}To view a complete example, check out the last step of this recipe.
6. Show banner ad
#Once you have a loaded instance ofBannerAd, useAdWidget to show it.
AdWidget(ad:_bannerAd) It's a good idea to wrap the widget in aSafeArea (so that no part of the ad is obstructed by device notches) and aSizedBox (so that it has its specified, constant size before and after loading).
@overrideWidgetbuild(BuildContextcontext){returnSafeArea(child:SizedBox(width:widget.adSize.width.toDouble(),height:widget.adSize.height.toDouble(),child:_bannerAd==null// Nothing to render yet.?constSizedBox()// The actual ad.:AdWidget(ad:_bannerAd!),),);} You must dispose of an ad when you no longer need to access it. The best practice for when to calldispose() is either after theAdWidget is removed from the widget tree or in theBannerAdListener.onAdFailedToLoad() callback.
_bannerAd?.dispose();7. Configure ads
#To show anything beyond test ads, you have to register ad units.
OpenAdMob.
Create anAd unit for each of the AdMob apps.

This asks for the Ad unit's format. AdMob provides many formats beyond banner ads --- interstitials, rewarded ads, app open ads, and so on. The API for those is similar, and documented in theAdMob documentation and throughofficial samples.
Choose banner ads.
Get theAd unit IDs for both the Android app and the iOS app. You can find these in theAd units section. They look something like
ca-app-pub-1234567890123456/1234567890. The format resembles theApp ID but with a slash (/) between the two numbers. This distinguishes anAd unit ID from anApp ID.
Add theseAd unit IDs to the constructor of
BannerAd, depending on the target app platform.dartfinalStringadUnitId=Platform.isAndroid// Use this ad unit on Android...?'ca-app-pub-3940256099942544/6300978111'// ... or this one on iOS.:'ca-app-pub-3940256099942544/2934735716';
8. Final touches
#To display the ads in a published app or game (as opposed to debug or testing scenarios), your app must meet additional requirements:
Your app must be reviewed and approved before it can fully serve ads. Follow AdMob'sapp readiness guidelines. For example, your app must be listed on at least one of the supported stores such as Google Play Store or Apple App Store.
You mustcreate an
app-ads.txtfile and publish it on your developer website.

To learn more about app and game monetization, visit the official sites ofAdMob andAd Manager.
9. Complete example
#The following code implements a simple stateful widget that loads a banner ad and shows it.
import'dart:io';import'package:flutter/widgets.dart';import'package:google_mobile_ads/google_mobile_ads.dart';classMyBannerAdWidgetextendsStatefulWidget{/// The requested size of the banner. Defaults to [AdSize.banner].finalAdSizeadSize;/// The AdMob ad unit to show.////// TODO: replace this test ad unit with your own ad unitfinalStringadUnitId=Platform.isAndroid// Use this ad unit on Android...?'ca-app-pub-3940256099942544/6300978111'// ... or this one on iOS.:'ca-app-pub-3940256099942544/2934735716';MyBannerAdWidget({super.key,this.adSize=AdSize.banner});@overrideState<MyBannerAdWidget>createState()=>_MyBannerAdWidgetState();}class_MyBannerAdWidgetStateextendsState<MyBannerAdWidget>{/// The banner ad to show. This is `null` until the ad is actually loaded.BannerAd?_bannerAd;@overrideWidgetbuild(BuildContextcontext){returnSafeArea(child:SizedBox(width:widget.adSize.width.toDouble(),height:widget.adSize.height.toDouble(),child:_bannerAd==null// Nothing to render yet.?constSizedBox()// The actual ad.:AdWidget(ad:_bannerAd!),),);}@overridevoidinitState(){super.initState();_loadAd();}@overridevoiddispose(){_bannerAd?.dispose();super.dispose();}/// Loads a banner ad.void_loadAd(){finalbannerAd=BannerAd(size:widget.adSize,adUnitId:widget.adUnitId,request:constAdRequest(),listener:BannerAdListener(// Called when an ad is successfully received.onAdLoaded:(ad){if(!mounted){ad.dispose();return;}setState((){_bannerAd=adasBannerAd;});},// Called when an ad request failed.onAdFailedToLoad:(ad,error){debugPrint('BannerAd failed to load:$error');ad.dispose();},),);// Start loading.bannerAd.load();}}In many cases, you will want to load the adoutside a widget.
For example, you can load it in aChangeNotifier, a BLoC, a controller, or whatever else you are using for app-level state. This way, you can preload a banner ad in advance, and have it ready to show for when the user navigates to a new screen.
Verify that you have loaded theBannerAd instance before showing it with anAdWidget, and that you dispose of the instance when it is no longer needed.
Unless stated otherwise, the documentation on this site reflects Flutter 3.38.1. Page last updated on 2025-10-30.View source orreport an issue.