Updated 27 May 2022
Google’s AdMob is a mobile advertising network for app developers. It allows you to monetize your app and begin making money from it by displaying Google adverts in your app. AdMobs in flutter is the preferred option for developers since it is simple and free to implement, does the most of the work for you, and allows you to generate money without having to learn difficult coding. It is completely configurable; you can choose where and what type of adverts to display.
You’ll need a Google account to get started with AdMobs in flutter, which will be required to log into the AdMob interface. You must first add your app and then generate some ad units for use in your app. It’s simple to make ad units. All you have to do is follow Google’s guidelines, but in this blog, we’ll focus on how to incorporate them into your Flutter application.Flutter currently supports loading and displaying banner, interstitial (full-screen), native ads, and rewarded video ads.
You have complete control over the adverts that appear in your app. You can do so by going to the AdMob console and selecting your app. Then go to “Blocking Controls” to get a list of all the different sorts of advertising that can be displayed in your app. You can turn off the advertising you don’t want with the switch provided.
1) Project Setup:
Create a new flutter project and add latest google_mobile_ads version under dependencies in pubspec.yaml file of your project as following example and run flutter pub get command to install the package in your project.
1 2 3 |
dependencies: google_mobile_ads: ^1.2.0 |
2) Import Package:
Import google_mobile_ads package in your class by using following code.
1 |
import 'package:google_mobile_ads/google_mobile_ads.dart'; |
3) Initialize AdMobs:
You need to initialize Mobile Ads. You can do this in your main method of you main class or any time before creating an ad, but just once. Example.
1 2 3 4 5 |
void main() { WidgetsFlutterBinding.ensureInitialized(); MobileAds.instance.initialize(); runApp(MyApp()); } |
4.) Register Your Device:
In case you are using test ads which you should while developing the app. You need to register your device to show test apps. Otherwise, your apps will be not be displayed. Below provided code segment helps you to do this.
1 |
const String testDevice = 'YOUR_DEVICE_ID'; |
5. Platform Specific:
Android: Open the android/app/src/main/AndroidManifest.xml file in Android Studio.
Add your AdMob app ID by adding a tag with the name com.google.android.gms.ads.APPLICATION_ID. For example, if your AdMob app ID is ca-app-pub-3940256099942544~3347511713, then you need to add the following lines to the AndroidManifest.xml file.
1 2 3 4 5 6 7 8 9 10 |
<manifest> ... <application> ... <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> </application> </manifest> |
iOS: Open the ios/Runner/Info.plist file in Android Studio.
Add a GADApplicationIdentifier key with the string value of your AdMob app ID. For example, if your AdMob app ID is ca-app-pub-3940256099942544~1458002511, then you need to add the following lines to the Info.plist file.
1 2 3 4 |
... <key>GADApplicationIdentifier</key> <string>ca-app-pub-3940256099942544~1458002511</string> ... |
6. Complete Implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ignore_for_file: public_member_api_docs import 'dart:io' show Platform; import 'package:flutter/material.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'anchored_adaptive_example.dart'; import 'fluid_example.dart'; import 'inline_adaptive_example.dart'; import 'reusable_inline_example.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); MobileAds.instance.initialize(); runApp(MyApp()); } // You can also test with your own ad unit IDs by registering your device as a // test device. Check the logs for your device's ID value. const String testDevice = 'YOUR_DEVICE_ID'; const int maxFailedLoadAttempts = 3; class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { static final AdRequest request = AdRequest( keywords: <String>['foo', 'bar'], contentUrl: 'http://foo.com/bar.html', nonPersonalizedAds: true, ); InterstitialAd? _interstitialAd; int _numInterstitialLoadAttempts = 0; RewardedAd? _rewardedAd; int _numRewardedLoadAttempts = 0; RewardedInterstitialAd? _rewardedInterstitialAd; int _numRewardedInterstitialLoadAttempts = 0; @override void initState() { super.initState(); _createInterstitialAd(); _createRewardedAd(); _createRewardedInterstitialAd(); } void _createInterstitialAd() { InterstitialAd.load( adUnitId: Platform.isAndroid ? 'ca-app-pub-3940256099942544/1033173712' : 'ca-app-pub-3940256099942544/4411468910', request: request, adLoadCallback: InterstitialAdLoadCallback( onAdLoaded: (InterstitialAd ad) { print('$ad loaded'); _interstitialAd = ad; _numInterstitialLoadAttempts = 0; _interstitialAd!.setImmersiveMode(true); }, onAdFailedToLoad: (LoadAdError error) { print('InterstitialAd failed to load: $error.'); _numInterstitialLoadAttempts += 1; _interstitialAd = null; if (_numInterstitialLoadAttempts < maxFailedLoadAttempts) { _createInterstitialAd(); } }, )); } void _showInterstitialAd() { if (_interstitialAd == null) { print('Warning: attempt to show interstitial before loaded.'); return; } _interstitialAd!.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (InterstitialAd ad) => print('ad onAdShowedFullScreenContent.'), onAdDismissedFullScreenContent: (InterstitialAd ad) { print('$ad onAdDismissedFullScreenContent.'); ad.dispose(); _createInterstitialAd(); }, onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) { print('$ad onAdFailedToShowFullScreenContent: $error'); ad.dispose(); _createInterstitialAd(); }, ); _interstitialAd!.show(); _interstitialAd = null; } void _createRewardedAd() { RewardedAd.load( adUnitId: Platform.isAndroid ? 'ca-app-pub-3940256099942544/5224354917' : 'ca-app-pub-3940256099942544/1712485313', request: request, rewardedAdLoadCallback: RewardedAdLoadCallback( onAdLoaded: (RewardedAd ad) { print('$ad loaded.'); _rewardedAd = ad; _numRewardedLoadAttempts = 0; }, onAdFailedToLoad: (LoadAdError error) { print('RewardedAd failed to load: $error'); _rewardedAd = null; _numRewardedLoadAttempts += 1; if (_numRewardedLoadAttempts < maxFailedLoadAttempts) { _createRewardedAd(); } }, )); } void _showRewardedAd() { if (_rewardedAd == null) { print('Warning: attempt to show rewarded before loaded.'); return; } _rewardedAd!.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (RewardedAd ad) => print('ad onAdShowedFullScreenContent.'), onAdDismissedFullScreenContent: (RewardedAd ad) { print('$ad onAdDismissedFullScreenContent.'); ad.dispose(); _createRewardedAd(); }, onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) { print('$ad onAdFailedToShowFullScreenContent: $error'); ad.dispose(); _createRewardedAd(); }, ); _rewardedAd!.setImmersiveMode(true); _rewardedAd!.show( onUserEarnedReward: (AdWithoutView ad, RewardItem reward) { print('$ad with reward $RewardItem(${reward.amount}, ${reward.type})'); }); _rewardedAd = null; } void _createRewardedInterstitialAd() { RewardedInterstitialAd.load( adUnitId: Platform.isAndroid ? 'ca-app-pub-3940256099942544/5354046379' : 'ca-app-pub-3940256099942544/6978759866', request: request, rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback( onAdLoaded: (RewardedInterstitialAd ad) { print('$ad loaded.'); _rewardedInterstitialAd = ad; _numRewardedInterstitialLoadAttempts = 0; }, onAdFailedToLoad: (LoadAdError error) { print('RewardedInterstitialAd failed to load: $error'); _rewardedInterstitialAd = null; _numRewardedInterstitialLoadAttempts += 1; if (_numRewardedInterstitialLoadAttempts < maxFailedLoadAttempts) { _createRewardedInterstitialAd(); } }, )); } void _showRewardedInterstitialAd() { if (_rewardedInterstitialAd == null) { print('Warning: attempt to show rewarded interstitial before loaded.'); return; } _rewardedInterstitialAd!.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (RewardedInterstitialAd ad) => print('$ad onAdShowedFullScreenContent.'), onAdDismissedFullScreenContent: (RewardedInterstitialAd ad) { print('$ad onAdDismissedFullScreenContent.'); ad.dispose(); _createRewardedInterstitialAd(); }, onAdFailedToShowFullScreenContent: (RewardedInterstitialAd ad, AdError error) { print('$ad onAdFailedToShowFullScreenContent: $error'); ad.dispose(); _createRewardedInterstitialAd(); }, ); _rewardedInterstitialAd!.setImmersiveMode(true); _rewardedInterstitialAd!.show( onUserEarnedReward: (AdWithoutView ad, RewardItem reward) { print('$ad with reward $RewardItem(${reward.amount}, ${reward.type})'); }); _rewardedInterstitialAd = null; } @override void dispose() { super.dispose(); _interstitialAd?.dispose(); _rewardedAd?.dispose(); _rewardedInterstitialAd?.dispose(); } @override Widget build(BuildContext context) { return MaterialApp( home: Builder(builder: (BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('AdMob Plugin example app'), actions: <Widget>[ PopupMenuButton<String>( onSelected: (String result) { switch (result) { case 'InterstitialAd': _showInterstitialAd(); break; case 'RewardedAd': _showRewardedAd(); break; case 'RewardedInterstitialAd': _showRewardedInterstitialAd(); break; case 'Fluid': Navigator.push( context, MaterialPageRoute(builder: (context) => FluidExample()), ); break; case 'Inline adaptive': Navigator.push( context, MaterialPageRoute( builder: (context) => InlineAdaptiveExample()), ); break; case 'Anchored adaptive': Navigator.push( context, MaterialPageRoute( builder: (context) => AnchoredAdaptiveExample()), ); break; default: throw AssertionError('unexpected button: $result'); } }, itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[ PopupMenuItem<String>( value: 'InterstitialAd', child: Text('InterstitialAd'), ), PopupMenuItem<String>( value: 'RewardedAd', child: Text('RewardedAd'), ), PopupMenuItem<String>( value: 'RewardedInterstitialAd', child: Text('RewardedInterstitialAd'), ), PopupMenuItem<String>( value: 'Fluid', child: Text('Fluid'), ), PopupMenuItem<String>( value: 'Inline adaptive', child: Text('Inline adaptive'), ), PopupMenuItem<String>( value: 'Anchored adaptive', child: Text('Anchored adaptive'), ), ], ), ], ), body: SafeArea(child: ReusableInlineExample()), ); }), ); } }// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ignore_for_file: public_member_api_docs import 'dart:io' show Platform; import 'package:flutter/material.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'anchored_adaptive_example.dart'; import 'fluid_example.dart'; import 'inline_adaptive_example.dart'; import 'reusable_inline_example.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); MobileAds.instance.initialize(); runApp(MyApp()); } // You can also test with your own ad unit IDs by registering your device as a // test device. Check the logs for your device's ID value. const String testDevice = 'YOUR_DEVICE_ID'; const int maxFailedLoadAttempts = 3; class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { static final AdRequest request = AdRequest( keywords: <String>['foo', 'bar'], contentUrl: 'http://foo.com/bar.html', nonPersonalizedAds: true, ); InterstitialAd? _interstitialAd; int _numInterstitialLoadAttempts = 0; RewardedAd? _rewardedAd; int _numRewardedLoadAttempts = 0; RewardedInterstitialAd? _rewardedInterstitialAd; int _numRewardedInterstitialLoadAttempts = 0; @override void initState() { super.initState(); _createInterstitialAd(); _createRewardedAd(); _createRewardedInterstitialAd(); } void _createInterstitialAd() { InterstitialAd.load( adUnitId: Platform.isAndroid ? 'ca-app-pub-3940256099942544/1033173712' : 'ca-app-pub-3940256099942544/4411468910', request: request, adLoadCallback: InterstitialAdLoadCallback( onAdLoaded: (InterstitialAd ad) { print('$ad loaded'); _interstitialAd = ad; _numInterstitialLoadAttempts = 0; _interstitialAd!.setImmersiveMode(true); }, onAdFailedToLoad: (LoadAdError error) { print('InterstitialAd failed to load: $error.'); _numInterstitialLoadAttempts += 1; _interstitialAd = null; if (_numInterstitialLoadAttempts < maxFailedLoadAttempts) { _createInterstitialAd(); } }, )); } void _showInterstitialAd() { if (_interstitialAd == null) { print('Warning: attempt to show interstitial before loaded.'); return; } _interstitialAd!.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (InterstitialAd ad) => print('ad onAdShowedFullScreenContent.'), onAdDismissedFullScreenContent: (InterstitialAd ad) { print('$ad onAdDismissedFullScreenContent.'); ad.dispose(); _createInterstitialAd(); }, onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) { print('$ad onAdFailedToShowFullScreenContent: $error'); ad.dispose(); _createInterstitialAd(); }, ); _interstitialAd!.show(); _interstitialAd = null; } void _createRewardedAd() { RewardedAd.load( adUnitId: Platform.isAndroid ? 'ca-app-pub-3940256099942544/5224354917' : 'ca-app-pub-3940256099942544/1712485313', request: request, rewardedAdLoadCallback: RewardedAdLoadCallback( onAdLoaded: (RewardedAd ad) { print('$ad loaded.'); _rewardedAd = ad; _numRewardedLoadAttempts = 0; }, onAdFailedToLoad: (LoadAdError error) { print('RewardedAd failed to load: $error'); _rewardedAd = null; _numRewardedLoadAttempts += 1; if (_numRewardedLoadAttempts < maxFailedLoadAttempts) { _createRewardedAd(); } }, )); } void _showRewardedAd() { if (_rewardedAd == null) { print('Warning: attempt to show rewarded before loaded.'); return; } _rewardedAd!.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (RewardedAd ad) => print('ad onAdShowedFullScreenContent.'), onAdDismissedFullScreenContent: (RewardedAd ad) { print('$ad onAdDismissedFullScreenContent.'); ad.dispose(); _createRewardedAd(); }, onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) { print('$ad onAdFailedToShowFullScreenContent: $error'); ad.dispose(); _createRewardedAd(); }, ); _rewardedAd!.setImmersiveMode(true); _rewardedAd!.show( onUserEarnedReward: (AdWithoutView ad, RewardItem reward) { print('$ad with reward $RewardItem(${reward.amount}, ${reward.type})'); }); _rewardedAd = null; } void _createRewardedInterstitialAd() { RewardedInterstitialAd.load( adUnitId: Platform.isAndroid ? 'ca-app-pub-3940256099942544/5354046379' : 'ca-app-pub-3940256099942544/6978759866', request: request, rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback( onAdLoaded: (RewardedInterstitialAd ad) { print('$ad loaded.'); _rewardedInterstitialAd = ad; _numRewardedInterstitialLoadAttempts = 0; }, onAdFailedToLoad: (LoadAdError error) { print('RewardedInterstitialAd failed to load: $error'); _rewardedInterstitialAd = null; _numRewardedInterstitialLoadAttempts += 1; if (_numRewardedInterstitialLoadAttempts < maxFailedLoadAttempts) { _createRewardedInterstitialAd(); } }, )); } void _showRewardedInterstitialAd() { if (_rewardedInterstitialAd == null) { print('Warning: attempt to show rewarded interstitial before loaded.'); return; } _rewardedInterstitialAd!.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (RewardedInterstitialAd ad) => print('$ad onAdShowedFullScreenContent.'), onAdDismissedFullScreenContent: (RewardedInterstitialAd ad) { print('$ad onAdDismissedFullScreenContent.'); ad.dispose(); _createRewardedInterstitialAd(); }, onAdFailedToShowFullScreenContent: (RewardedInterstitialAd ad, AdError error) { print('$ad onAdFailedToShowFullScreenContent: $error'); ad.dispose(); _createRewardedInterstitialAd(); }, ); _rewardedInterstitialAd!.setImmersiveMode(true); _rewardedInterstitialAd!.show( onUserEarnedReward: (AdWithoutView ad, RewardItem reward) { print('$ad with reward $RewardItem(${reward.amount}, ${reward.type})'); }); _rewardedInterstitialAd = null; } @override void dispose() { super.dispose(); _interstitialAd?.dispose(); _rewardedAd?.dispose(); _rewardedInterstitialAd?.dispose(); } @override Widget build(BuildContext context) { return MaterialApp( home: Builder(builder: (BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('AdMob Plugin example app'), actions: <Widget>[ PopupMenuButton<String>( onSelected: (String result) { switch (result) { case 'InterstitialAd': _showInterstitialAd(); break; case 'RewardedAd': _showRewardedAd(); break; case 'RewardedInterstitialAd': _showRewardedInterstitialAd(); break; case 'Fluid': Navigator.push( context, MaterialPageRoute(builder: (context) => FluidExample()), ); break; case 'Inline adaptive': Navigator.push( context, MaterialPageRoute( builder: (context) => InlineAdaptiveExample()), ); break; case 'Anchored adaptive': Navigator.push( context, MaterialPageRoute( builder: (context) => AnchoredAdaptiveExample()), ); break; default: throw AssertionError('unexpected button: $result'); } }, itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[ PopupMenuItem<String>( value: 'InterstitialAd', child: Text('InterstitialAd'), ), PopupMenuItem<String>( value: 'RewardedAd', child: Text('RewardedAd'), ), PopupMenuItem<String>( value: 'RewardedInterstitialAd', child: Text('RewardedInterstitialAd'), ), PopupMenuItem<String>( value: 'Fluid', child: Text('Fluid'), ), PopupMenuItem<String>( value: 'Inline adaptive', child: Text('Inline adaptive'), ), PopupMenuItem<String>( value: 'Anchored adaptive', child: Text('Anchored adaptive'), ), ], ), ], ), body: SafeArea(child: ReusableInlineExample()), ); }), ); } } |
7. Dispose:
Dispose all mobile add after the function work to prevent memory leak as following code.
1 2 3 4 5 6 7 |
@override void dispose() { super.dispose(); _interstitialAd?.dispose(); _rewardedAd?.dispose(); _rewardedInterstitialAd?.dispose(); } |
In this article, we have learned about various types of Ads provided by google in flutter application using google_mobile_ads plugin.
Thanks for reading the blog. For more such amazing articles on latest trends in mobile application development please visit our mobikul blog site.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.