'Flutter: Show Interstitial Ads after Splash Screen
in my Flutter APP, when a user launches the app, a splash screen is shown after that he is forwarded to the homeScreen.
my goal is to show an admob interstitial ad between the splash screen and the homescreen.
something like this : see this post from google

i have tried many options, but unfortunately without luck.
does anyone have a code / idea how to implement this in flutter.
thank you in advance.
Solution 1:[1]
I hope this helps:
There are 2 scenarios where user will be navigated to the home screen:
- Either user closes the ad after the ad is shown.
- Or ad fails to load.
class SplashScreen extends StatefulWidget {
SplashScreen({Key? key}) : super(key: key);
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
_createInterstitialAd();
super.initState();
}
@override
void dispose() {
_interstitialAd?.dispose();
super.dispose();
}
InterstitialAd? _interstitialAd;
int _numInterstitialLoadAttempts = 0;
int maxFailedLoadAttempts = 3;
void _createInterstitialAd() {
InterstitialAd.load(
adUnitId: "YOUR-AD-UNIT-ID",
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
_interstitialAd = ad;
_numInterstitialLoadAttempts = 0;
_showInterstitialAd();
},
onAdFailedToLoad: (LoadAdError error) {
_numInterstitialLoadAttempts += 1;
_interstitialAd = null;
if (_numInterstitialLoadAttempts < maxFailedLoadAttempts) {
_createInterstitialAd();
} else {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false,
);
}
},
),
);
}
void _showInterstitialAd() {
if (_interstitialAd == null) {
print('Warning: attempt to show interstitial before loaded.');
return;
}
_interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdDismissedFullScreenContent: (InterstitialAd ad) {
ad.dispose();
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false,
);
},
);
_interstitialAd!.show();
_interstitialAd = null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Text("Your Splash Screen"),
),
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Talha |
