How to show interstitial ads evry 20 seconds in flutter dart
3,319
Try to add the following to your code:
import 'dart:async'; // <-- put it on very top of your file
Timer _timerForInter; // <- Put this line on top of _MyAppState class
@override
void initState() {
// Add these lines to launch timer on start of the app
_timerForInter = Timer.periodic(Duration(seconds: 20), (result) {
_interstitialAd = createInterstitialAd()..load();
});
super.initState();
}
@override
void dispose() {
// Add these to dispose to cancel timer when user leaves the app
_timerForInter.cancel();
_interstitialAd.dispose();
super.dispose();
}
Author by
Joseph M
Updated on December 19, 2022Comments
-
Joseph M over 1 year
i have a problem implementing interstitial timed ads with flutter. i have implemented one option which shows ads when a button is clicked but i want to implement where it shows ad every 20 seconds.please help
am using the code below i found online
import 'package:flutter/material.dart'; import 'package:firebase_admob/firebase_admob.dart'; const String testDevice = 'MobileId'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo( testDevices: testDevice != null ? <String>[testDevice] : null, nonPersonalizedAds: true, keywords: <String>['Game', 'Mario'], ); BannerAd _bannerAd; InterstitialAd _interstitialAd; BannerAd createBannerAd() { return BannerAd( adUnitId: BannerAd.testAdUnitId, //Change BannerAd adUnitId with Admob ID size: AdSize.banner, targetingInfo: targetingInfo, listener: (MobileAdEvent event) { print("BannerAd $event"); }); } InterstitialAd createInterstitialAd() { return InterstitialAd( adUnitId: InterstitialAd.testAdUnitId, //Change Interstitial AdUnitId with Admob ID targetingInfo: targetingInfo, listener: (MobileAdEvent event) { print("IntersttialAd $event"); }); } @override void initState() { FirebaseAdMob.instance.initialize(appId: BannerAd.testAdUnitId); //Change appId With Admob Id _bannerAd = createBannerAd() ..load() ..show(); super.initState(); } @override void dispose() { _bannerAd.dispose(); _interstitialAd.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData(), home: Scaffold( appBar: AppBar( title: Text("Demo App"), ), body: Center( child: RaisedButton( child: Text('Click on Ads'), onPressed: () { createInterstitialAd() ..load() ..show(); }, )), ), ); } }
am using the flutter language and there are not many tutorials online yet because the community is still growing
-
Joseph M about 4 yearsit says class not found when i put Timer _timerForInter; // <- Put this line on top of _MyAppState class
-
emvaized about 4 years@josephmuchoki I guess you have to add 'import 'dart:async'; " on very top of your file.
-
Joseph M about 4 yearsthanks, it worked. one spelling error from your code i have rectified, and a missing semicolon... check full code below
-
Joseph M about 4 years
import 'dart:async'; Timer _timerForInter; @override void initState() { FirebaseAdMob.instance.initialize(appId: BannerAd.testAdUnitId); //Change appId With Admob Id _bannerAd = createBannerAd() ..load() ..show(); _timerForInter = Timer.periodic(Duration(seconds: 20), (result) { _interstitialAd = createInterstitialAd()..load(); }); super.initState(); } @override void dispose() { _bannerAd.dispose(); _timerForInter.cancel(); _interstitialAd.dispose(); super.dispose(); }
-
Joseph M about 4 yearsits not aligning but those are the changes
-
emvaized about 4 years@josephmuchoki Oh, sorry - I wrote it from my phone; fixed typos now. Please mark my answer as correct if it solves your problem :)