I included ads in my Flutter project. When I click "Load ads" button first time on Android emulator ad loads and shows perfectly. But when I close the ad and click "Load ads" button again I get this exception:
I/flutter ( 1549): InterstitialAd event MobileAdEvent.closed D/DynamitePackage( 1549): Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl I/Ads ( 1549): This request is sent from a test device. W/flutter ( 1549): onAdFailedToLoad: 3 I/Ads ( 1549): Ad failed to load : 3 I/flutter ( 1549): InterstitialAd event MobileAdEvent.failedToLoad E/flutter ( 1549): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(load_failed_ad, cannot reload a failed ad, id=571273309, null)
Flutter doctor:
[√] Flutter (Channel stable, v1.9.1+hotfix.6, on Microsoft Windows [Version 10.0.18362.418], locale en-US) • Flutter version 1.9.1+hotfix.6 at C:\Users\olgam\source\flutter • Framework revision 68587a0916 (9 weeks ago), 2019-09-13 19:46:58 -0700 • Engine revision b863200c37 • Dart version 2.5.0
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3) • Android SDK at C:\Users\olgam\AppData\Local\Android\sdk • Android NDK location not configured (optional; useful for native profiling support) • Platform android-28, build-tools 28.0.3 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03) • All Android licenses accepted.
[√] Android Studio (version 3.5) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin version 41.0.2 • Dart plugin version 191.8593 • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[√] Connected device (2 available) • SM G930U • d77c5f0b • android-arm64 • Android 8.0.0 (API 26) • Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)
• No issues found!
Here is my code:
class MainPage extends StatefulWidget {
final String _locale;
MainPage(this._locale);
@override
State<StatefulWidget> createState() {
return MainPageState();
}
}
class MainPageState extends State<MainPage> {
InterstitialAd _interstitialAd;
static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
childDirected: true, keywords: ['Games', 'Puzzles', 'Kids']);
InterstitialAd buildInterstitial() {
return InterstitialAd(
adUnitId: Constants.UNIT_ID,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("InterstitialAd event $event");
if (event == MobileAdEvent.failedToLoad) {
_interstitialAd.load();
} else if (event == MobileAdEvent.closed) {
_interstitialAd = buildInterstitial()..load();
}
},
);
}
@override
void initState() {
FirebaseAdMob.instance.initialize(appId: Constants.APP_ID);
_interstitialAd = buildInterstitial()..load();
super.initState();
}
@override
void dispose() {
_interstitialAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Kids Development'),
),
body: Container(
width: double.infinity,
margin: EdgeInsets.all(15.0),
child: RaisedButton(
child: Text('Load ads'),
onPressed: () {
//print('--------trying to build interstitial');
//_interstitialAd = buildInterstitial();
_interstitialAd
..load()
..show();
},
)
);
}
}
This happens only with real ad unit id. If I use InterstitialAd.testAdUnitId everythins works fine.