Hello from Turkey. I added admob ads to my Flutter application.There is an ad on each page in the 2-page application and there is such a situation that the ad on the main page stays open even if the page changes. But the ad on the 2nd page only opens when the page is changed. Is there a mistake here, is there a problem or does it work like this? I also examined the documentation and there is nothing missing in my code.
example code :
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(const MaterialApp(
title: 'Navigation Basics',
home: FirstRoute(),
));
}
class FirstRoute extends StatefulWidget {
const FirstRoute({super.key});
@override
State<FirstRoute> createState() => _FirstRouteState();
}
class _FirstRouteState extends State<FirstRoute> {
late BannerAd? _bannerAd;
bool _isBannerAdReady = false;
void _loadBannerAd() {
_bannerAd = BannerAd(
adUnitId: 'ca-app-pub-3940256099942544/6300978111',
size: AdSize.banner,
request: AdRequest(),
listener: BannerAdListener(
onAdLoaded: (_) {
setState(() {
_isBannerAdReady = true;
});
},
onAdFailedToLoad: (ad, error) {
_bannerAd!.dispose();
},
),
);
_bannerAd!.load();
}
@override
void initState() {
super.initState();
_loadBannerAd();
}
void dispose() {
super.dispose();
_bannerAd!.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First Route'),
),
body: Center(
child: ElevatedButton(
child: const Text('Open route'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()),
);
},
),
),
bottomNavigationBar: _isBannerAdReady
? SizedBox(
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
child: AdWidget(ad: _bannerAd!))
: SizedBox(
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
),
);
}
}
class SecondRoute extends StatefulWidget {
const SecondRoute({super.key});
@override
State<SecondRoute> createState() => _SecondRouteState();
}
class _SecondRouteState extends State<SecondRoute> {
late BannerAd? _bannerAd;
bool _isBannerAdReady = false;
void _loadBannerAd() {
_bannerAd = BannerAd(
adUnitId: 'ca-app-pub-3940256099942544/6300978111',
size: AdSize.banner,
request: AdRequest(),
listener: BannerAdListener(
onAdLoaded: (_) {
setState(() {
_isBannerAdReady = true;
});
},
onAdFailedToLoad: (ad, error) {
_bannerAd!.dispose();
},
),
);
_bannerAd!.load();
}
@override
void initState() {
super.initState();
_loadBannerAd();
}
void dispose() {
super.dispose();
_bannerAd!.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Route'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go back!'),
),
),
bottomNavigationBar: _isBannerAdReady
? SizedBox(
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
child: AdWidget(ad: _bannerAd!))
: SizedBox(
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
),
);
}
}