text stringlengths 1 474 |
|---|
<topic_end> |
<topic_start>Add ads to your mobile Flutter app or game |
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, use |
AdMob, |
Google’s mobile advertising platform. |
This recipe demonstrates how to use the |
google_mobile_ads |
package to add a banner ad to your app or game.info Note |
Apart from AdMob, the google_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 the |
Ad Manager documentation.<topic_end> |
<topic_start> |
1. Get AdMob App IDs |
Go to AdMob 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 two Apps in AdMob: one for |
Android and one for iOS.Open the App settings section.Get the AdMob App IDs for both the Android app and the iOS app. |
They resemble ca-app-pub-1234567890123456~1234567890. Note the |
tilde (~) between the two numbers.<topic_end> |
<topic_start> |
2. Platform-specific setup |
Update your Android and iOS configurations to include your App IDs.<topic_end> |
<topic_start> |
Android |
Add your AdMob app ID to your Android app.Open the app’s android/app/src/main/AndroidManifest.xml file.Add a new <meta-data> tag.Set the android:name element with a value of |
com.google.android.gms.ads.APPLICATION_ID.Set the android:value element with the value to your own AdMob app |
ID that you got in the previous step. |
Include them in quotes as shown:<topic_end> |
<topic_start> |
iOS |
Add your AdMob app ID to your iOS app.Open your app’s ios/Runner/Info.plist file.Enclose GADApplicationIdentifier with a key tag.Enclose your AdMob app ID with a string tag. You created this AdMob |
App ID in step 1.<topic_end> |
<topic_start> |
3. Add the google_mobile_ads plugin |
To add the google_mobile_ads plugin as a dependency, run |
flutter pub add:info Note |
Once you add the plugin, your Android app might fail to build with a |
DexArchiveMergerException:To resolve this, execute the flutter run command in the terminal, not |
through an IDE plugin. The flutter tool can detect the issue and ask |
whether it should try to solve it. Answer y, and the problem goes away. |
You can return to running your app from an IDE after that.<topic_end> |
<topic_start> |
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. |
<code_start>void main() async { |
WidgetsFlutterBinding.ensureInitialized(); |
unawaited(MobileAds.instance.initialize()); |
runApp(MyApp()); |
}<code_end> |
Run the initialization step at startup, as shown above, |
so that the AdMob SDK has enough time to initialize before it is needed.info Note |
MobileAds.instance.initialize() returns a Future but, the |
way the SDK is built, you don’t need to await it. |
If you try to load an ad before that Future is completed, |
the SDK will gracefully wait until the initialization, and then load the ad. |
You can await the Future |
if you want to know the exact time when the AdMob SDK is ready.<topic_end> |
<topic_start> |
5. Load a banner ad |
To show an ad, you need to request it from AdMob.To load a banner ad, construct a BannerAd instance, and |
call load() on it.info Note |
The following code snippet refers to fields such a adSize, adUnitId |
and _bannerAd. This will all make more sense in a later step. |
<code_start>/// Loads a banner ad. |
void _loadAd() { |
final bannerAd = BannerAd( |
size: widget.adSize, |
adUnitId: widget.adUnitId, |
request: const AdRequest(), |
listener: BannerAdListener( |
// Called when an ad is successfully received. |
onAdLoaded: (ad) { |
if (!mounted) { |
ad.dispose(); |
return; |
} |
setState(() { |
_bannerAd = ad as BannerAd; |
}); |
}, |
// Called when an ad request failed. |
onAdFailedToLoad: (ad, error) { |
debugPrint('BannerAd failed to load: $error'); |
ad.dispose(); |
}, |
), |
); |
// Start loading. |
bannerAd.load(); |
}<code_end> |
To view a complete example, check out the last step of this recipe.<topic_end> |
<topic_start> |
6. Show banner ad |
Once you have a loaded instance of BannerAd, use AdWidget to show it.It’s a good idea to wrap the widget in a SafeArea (so that no part of |
the ad is obstructed by device notches) and a SizedBox (so that it has |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.