text
stringlengths
1
372
<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
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.