googleads-mobile-flutter: šŸ›Ad for following adId already exists: 0

Bug report

Describe the bug This error appears if I set the adUnitId to literally anything. I’ve tried setting the property to ā€˜hi’ and a bunch of other stuff but it somehow still appears. And if I set it to a valid ad id it continues to appear and the ad never shows.

E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263): java.lang.IllegalArgumentException: Ad for following adId already exists: 0
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at io.flutter.plugins.googlemobileads.AdInstanceManager.trackAd(AdInstanceManager.java:66)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.onMethodCall(GoogleMobileAdsPlugin.java:266)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at android.os.MessageQueue.next(MessageQueue.java:335)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at android.os.Looper.loop(Looper.java:183)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at android.app.ActivityThread.main(ActivityThread.java:7656)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/MethodChannel#plugins.flutter.io/google_mobile_ads( 6263):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 6263): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(error, Ad for following adId already exists: 0, null, java.lang.IllegalArgumentException: Ad for following adId already exists: 0
E/flutter ( 6263):      at io.flutter.plugins.googlemobileads.AdInstanceManager.trackAd(AdInstanceManager.java:66)
E/flutter ( 6263):      at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.onMethodCall(GoogleMobileAdsPlugin.java:266)
E/flutter ( 6263):      at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter ( 6263):      at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter ( 6263):      at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/flutter ( 6263):      at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter ( 6263):      at android.os.MessageQueue.next(MessageQueue.java:335)
E/flutter ( 6263):      at android.os.Looper.loop(Looper.java:183)
E/flutter ( 6263):      at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter ( 6263):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 6263):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter ( 6263):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 6263): )
E/flutter ( 6263): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:582:7)
E/flutter ( 6263): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:18)
E/flutter ( 6263): <asynchronous suspension>
E/flutter ( 6263): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:332:12)
E/flutter ( 6263): #3      AdInstanceManager.loadNativeAd (package:google_mobile_ads/src/ad_instance_manager.dart:172:20)
E/flutter ( 6263): #4      NativeAd.load (package:google_mobile_ads/src/ad_containers.dart:576:27)
E/flutter ( 6263): #5      NativeAdState.initState.<anonymous closure> (package:discord_bots/widgets/global/native_ad_widget.dart:36:65)
E/flutter ( 6263): #6      new Future.delayed.<anonymous closure> (dart:async/future.dart:326:39)
E/flutter ( 6263): #7      _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 6263): #8      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 6263): #9      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 6263): #10     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 6263): #11     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 6263): #12     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 6263): #13     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter ( 6263): #14     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 6263): #15     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter ( 6263): #16     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter ( 6263): #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter ( 6263):

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a native ad widget
  2. Add the java files to the src folder and add the layout XML file to the res folder.
  3. Run app

Additional context

My code is from the example:


import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

class NativeAdWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => NativeAdState();
}

class NativeAdState extends State<NativeAdWidget> {
  NativeAd _nativeAd;
  final Completer<NativeAd> nativeAdCompleter = Completer<NativeAd>();

  @override
  void initState() {
    super.initState();
    _nativeAd = NativeAd(
      adUnitId: NativeAd.testAdUnitId,
      request: AdRequest(),
      factoryId: 'listTile',
      listener: AdListener(
        onAdLoaded: (Ad ad) {
          print('$NativeAd loaded.');
          nativeAdCompleter.complete(ad as NativeAd);
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('$NativeAd failedToLoad: $error');
          nativeAdCompleter.completeError(null);
        },
        onAdOpened: (Ad ad) => print('$NativeAd onAdOpened.'),
        onAdClosed: (Ad ad) => print('$NativeAd onAdClosed.'),
        onApplicationExit: (Ad ad) => print('$NativeAd onApplicationExit.'),
      ),
    );
    Future<void>.delayed(Duration(seconds: 1), () => _nativeAd?.load());
  }

  @override
  void dispose() {
    super.dispose();
    _nativeAd?.dispose();
    _nativeAd = null;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<NativeAd>(
      future: nativeAdCompleter.future,
      builder: (BuildContext context, AsyncSnapshot<NativeAd> snapshot) {
        Widget child;

        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
          case ConnectionState.active:
            child = Container();
            break;
          case ConnectionState.done:
            if (snapshot.hasData) {
              child = AdWidget(ad: _nativeAd);
            } else {
              child = Text('Error loading $NativeAd');
            }
        }

        return Container(
          width: 250,
          height: 350,
          child: child,
          color: Colors.blueGrey,
        );
      },
    );
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 13
  • Comments: 19

Most upvoted comments

Ads are loading and displaying when I run the app on a device or in the simulator but will crash if I press the ā€œRestartā€ button - not the ā€œHot Reloadā€ button.

Error message:

E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): Failed to handle method call
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): java.lang.IllegalArgumentException: Ad for following adId already exists: 0
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at io.flutter.plugins.googlemobileads.AdInstanceManager.trackAd(AdInstanceManager.java:66)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.onMethodCall(GoogleMobileAdsPlugin.java:244)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#plugins.flutter.io/google_mobile_ads(14986): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

The app crashes at the following line in the Widget: _bannerAd?.load();

Banner Widget code:

class TestBanner extends StatefulWidget {
  TestBanner({Key key}) : super(key: key);

  @override
  _TestBannerState createState() => _TestBannerState();
}

class _TestBannerState extends State<TestBanner> {
  BannerAd _bannerAd;

  @override
  void initState() {
    super.initState();
    _bannerAd = BannerAd(
      adUnitId: BannerAd.testAdUnitId,
      request: AdRequest(),
      size: AdSize.banner,
      listener: AdListener(
        onAdLoaded: (Ad ad) {
          print('$BannerAd loaded.');
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('$BannerAd failedToLoad: $error');
        },
        onAdOpened: (Ad ad) => print('$BannerAd onAdOpened.'),
        onAdClosed: (Ad ad) => print('$BannerAd onAdClosed.'),
        onApplicationExit: (Ad ad) => print('$BannerAd onApplicationExit.'),
      ),
    );
    _bannerAd?.load();
  }

  @override
  Widget build(BuildContext context) {
    final AdWidget adWidget = AdWidget(ad: _bannerAd);
    return Container(
      alignment: Alignment.center,
      child: adWidget,
      width: _bannerAd.size.width.toDouble(),
      height: _bannerAd.size.height.toDouble(),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _bannerAd?.dispose();
    _bannerAd = null;
  }
}

The crash on hot reload should be resolved in 0.11.0+4, via https://github.com/googleads/googleads-mobile-flutter/pull/94.

@WieFel If you’re having issue getting test ads to work could you open a separate issue?

After upgrading to the latest package I started getting this error:

java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.measurement.internal.zzjl@95dd0fd

I added this to my `build.gradle;’ which seems to have resolved the issue:

    implementation 'com.google.android.gms:play-services-basement:17.5.0'

If this is required shouldn’t it be included in the installation steps?

When I use the test ad unit I get this error after doing flutter run:

I/Ads     ( 3197): Ad failed to load : 3
I/flutter ( 3197): NativeAd failedToLoad: LoadAdError(code: 3, domain: com.google.android.gms.ads, message: No ad config.)

If I try to hot restart I get the Ad for following adId already exists: 0 error again. And yes I have followed the setup steps.

@jjliu15 apologies to tag you but is there any chance you or your colleagues could have a look at this issue? I have an app update that needs to go live but is currently blocked by this.