question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

šŸ›Ad for following adId already exists: 0

See original GitHub issue

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,
        );
      },
    );
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:18

github_iconTop GitHub Comments

6reactions
DennisH511commented, Mar 7, 2021

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;
  }
}
2reactions
jjliu15commented, Mar 22, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using test banner ad id in flutter throwing exception - "Ad for ...
I found out after research and testing that one unit ad needs a unique ad unit id. The unique identifier for an ad...
Read more >
[Solved]-Ads failed to load while using banner test ads-Flutter
Coding example for the question Ads failed to load while using banner test ... id in flutter throwing exception - "Ad for following...
Read more >
google_mobile_ads | Flutter Package - Pub.dev
Flutter plugin for Google Mobile Ads, supporting banner, interstitial (full-screen), rewarded and native ads.
Read more >
Get a user-resettable advertising ID - Android Developers
Note: Because the user can reset their advertising ID after your app starts, you should call AdvertisingIdClient.getAdvertisingIdInfo() eachĀ ...
Read more >
Banner Ads | Android - Google Developers
Note the following required attributes: ads:adSize - Set this to the ad size you'd like to use. If you don't want to use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found