Flutter and AdMob: A blog around monetizing your Flutter app with AdMob.
Integrate `Banner`, `Interstitial`, and `Rewarded` ads to your flutter application.

Search for a command to run...
Integrate `Banner`, `Interstitial`, and `Rewarded` ads to your flutter application.

No comments yet. Be the first to comment.
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders ↔ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts

Output of the starter project

These Posts are coming from the post.dart file and is displayed with the help of ListView.builder() widget.
google_mobile_ads package.
pubspec.yamldev_dependencies:
flutter_test:
sdk: flutter
google_mobile_ads: ^0.13.4







app ID and ad unit ID that you need to place when you are shipping your app in production.Don't use production
app idandunit idwhile developing apps. Use thetestkeys provided by the admob. You can get the test ids for Android here and for iOS here
GADApplicationIdentifier key with a string value of your AdMob app ID (identified in the AdMob UI).SKAdNetworkItems key with Google's SKAdNetworkIdentifier value of cstr6suwn9.skadnetwork.<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cstr6suwn9.skadnetwork</string>
</dict>
</array>
AndroidManifest.xml file and add the below code under the <application> tag<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
build.gradle and update the minSdkVersion to 19 or higher defaultConfig {
applicationId "com.example.google_ad"
minSdkVersion 19
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
main.dart and add the below code under main function.void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(const MyApp());
}
unit id according to platform.ad_state.dartand add the below code under it.class AdState {
String get bannerUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/6300978111"; // test unit id
} else {
return "ca-app-pub-3940256099942544/2934735716"; // test unit id
}
}
}
bannerUnitId getter will provide unitId for specific platform. You have to update this test id with your production unit ids when you deploy your app.HomePage.dart and add below code class _HomePageState extends State<HomePage> {
late BannerAd _bannerAd;
final bool _isBottomBannerAdLoaded = false;
....
_bannerAd override the initstate and add below code inside it.@override
void initState() {
super.initState();
_bottomBannerAd = BannerAd(
adUnitId: AdState.bannerUnitId,
size: AdSize.banner,
listener: BannerAdListener(
onAdLoaded: (_) {
setState(() {
_isBottomBannerAdLoaded = true;
});
},
onAdFailedToLoad: (ad, error) {
setState(() {
_isBottomBannerAdLoaded = false;
});
ad.dispose();
},
),
request: const AdRequest(),
)..load();
}
adUnitId gets our banner unit id from the class AdState that we've created earlier.size property takes the size of the ad. There are different sizes available. For example, fullBanner, largeBanner, leaderborad, etc.listener will receive notification for the lifecycle of a BannerId. onAdLoaded suggests Ad successfully loaded - display an AdWidget with the banner ad. onAdFailedToLoad suggests Ad failed to load - log the error and dispose the ad.request is used to request the ad for displaying it in the UI.BannerAd to the screen, the package provides us a widget called AdWidget._bottomBannerAd to AdWidget constructorScaffold(
bottomNavigationBar: _isBottomBannerAdLoaded
? SizedBox(
height: _bottomBannerAd.size.height.toDouble(),
width: _bottomBannerAd.size.width.toDouble(),
child: AdWidget(ad: _bottomBannerAd),
)
: Container(),
// .....


AdState file for getting the unitIDstatic String get interstitialAdUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/8691691433";
} else {
return "ca-app-pub-3940256099942544/5135589807";
}
}
InterstitialAd? _interstitialAd;
bool _isInterstitialAdLoaded = false;
load the InterstitialAd inside the initstate and also dispose the _interstitialAd inside dispose function.@override
void initState() {
super.initState();
InterstitialAd.load(
adUnitId: AdState.interstitialAdUnitId,
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
_interstitialAd = ad;
_isInterstitialAdLoaded = true;
},
onAdFailedToLoad: (LoadAdError error) {
_isInterstitialAdLoaded = false;
_interstitialAd.dispose();
},
),
);
}
@override
void dispose() {
_bottomBannerAd.dispose();
_interstitialAd.dispose();
super.dispose();
}
Now let's load this InterstitialAd when the user clicks on any of the posts.
show() method on Navigation, Just like belowGestureDetector(
onTap: () {
if (_isInterstitialAdLoaded) {
_interstitialAd.show(); // <- here
}
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => PostPage(
post: post,
),
),
);
},
child: PostCard(
hasReadMore: true,
post: post,
)
),

Woohoo!! 🎉


static String get rewardedAdUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/5224354917";
} else {
return "ca-app-pub-3940256099942544/1712485313";
}
}
late RewardedAd _rewardedAd;
bool _isRewardedAdLoaded= false;
initState@override
void initState() {
super.initState();
RewardedAd.load(
adUnitId: AdState.rewardedAdUnitId,
request: const AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (ad) {
_rewardedAd = ad;
ad.fullScreenContentCallback = FullScreenContentCallback(
onAdDismissedFullScreenContent: (ad) {
setState(() {
_isRewardedAdLoaded = false;
});
},
);
setState(() {
_isRewardedAdLoaded = true;
});
},
onAdFailedToLoad: (err) {
setState(() {
_isRewardedAdLoaded = false;
});
},
),
);
}
dispose() function@override
void dispose() {
_bottomBannerAd.dispose();
_interstitialAd.dispose();
_rewardedAd.dispose();
super.dispose();
}
show() this on FloatingActionButton clickScaffold(
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.gamepad),
onPressed: () {
_rewardedAd.show(
onUserEarnedReward: (ad, reward) {
// perform operation when earned rewards
},
);
},
),
//....


