text
stringlengths 1
474
|
|---|
its specified, constant size before and after loading).
|
<code_start>@override
|
Widget build(BuildContext context) {
|
return SafeArea(
|
child: SizedBox(
|
width: widget.adSize.width.toDouble(),
|
height: widget.adSize.height.toDouble(),
|
child: _bannerAd == null
|
// Nothing to render yet.
|
? SizedBox()
|
// The actual ad.
|
: AdWidget(ad: _bannerAd!),
|
),
|
);
|
}<code_end>
|
You must dispose of an ad when you no longer need to access it. The best
|
practice for when to call dispose() is either after the AdWidget is
|
removed from the widget tree or in the
|
BannerAdListener.onAdFailedToLoad() callback.
|
<code_start>_bannerAd?.dispose();<code_end>
|
<topic_end>
|
<topic_start>
|
7. Configure ads
|
To show anything beyond test ads, you have to register ad units.Open AdMob.Create an Ad 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 the
|
AdMob documentation
|
and through
|
official samples.Choose banner ads.Get the Ad unit IDs for both the Android app and the iOS app.
|
You can find these in the Ad units section. They look something
|
like ca-app-pub-1234567890123456/1234567890. The format resembles
|
the App ID but with a slash (/) between the two numbers. This
|
distinguishes an Ad unit ID from an App ID.Add these Ad unit IDs to the constructor of BannerAd,
|
depending on the target app platform.
|
<code_start>final String adUnitId = Platform.isAndroid
|
// Use this ad unit on Android...
|
? 'ca-app-pub-3940256099942544/6300978111'
|
// ... or this one on iOS.
|
: 'ca-app-pub-3940256099942544/2934735716';<code_end>
|
<topic_end>
|
<topic_start>
|
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’s app 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 must create an app-ads.txt
|
file and publish it on your developer website.To learn more about app and game monetization,
|
visit the official sites
|
of AdMob
|
and Ad Manager.<topic_end>
|
<topic_start>
|
9. Complete example
|
The following code implements a simple stateful widget that loads a
|
banner ad and shows it.
|
<code_start>import 'dart:io';
|
import 'package:flutter/widgets.dart';
|
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
class MyBannerAdWidget extends StatefulWidget {
|
/// The requested size of the banner. Defaults to [AdSize.banner].
|
final AdSize adSize;
|
/// The AdMob ad unit to show.
|
///
|
/// TODO: replace this test ad unit with your own ad unit
|
final String adUnitId = 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,
|
});
|
@override
|
State<MyBannerAdWidget> createState() => _MyBannerAdWidgetState();
|
}
|
class _MyBannerAdWidgetState extends State<MyBannerAdWidget> {
|
/// The banner ad to show. This is `null` until the ad is actually loaded.
|
BannerAd? _bannerAd;
|
@override
|
Widget build(BuildContext context) {
|
return SafeArea(
|
child: SizedBox(
|
width: widget.adSize.width.toDouble(),
|
height: widget.adSize.height.toDouble(),
|
child: _bannerAd == null
|
// Nothing to render yet.
|
? SizedBox()
|
// The actual ad.
|
: AdWidget(ad: _bannerAd!),
|
),
|
);
|
}
|
@override
|
void initState() {
|
super.initState();
|
_loadAd();
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.