text
stringlengths 1
372
|
|---|
<topic_end>
|
<topic_start>
|
more information
|
the flutter casual games toolkit includes the following templates:
|
<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.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.