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.

this ad may have not been loaded or has been disposed. native ads

See original GitHub issue

Trying to implement Native ads but it showing this error… “this ad may have not been loaded or has been disposed. Ad with the following id could not be found: 1 /6”

Java Code of android…

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        final NativeAdFactory factory = new NativeAdFactoryExample(getLayoutInflater());
        GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "adFactoryExample", factory);
        GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "listTile",
                new ListTileNativeAdFactory(getContext()));
    }

    @Override
    public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.cleanUpFlutterEngine(flutterEngine);
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample");
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "listTile");
    }
}

Do i need both listTile and adFactoryExample for Displaying Native and InlineNative?

Although i am not showing in same Page…

in HomePage showing with adFactoryExample…

  NativeAd _nativeAd;
  bool _nativeAdIsLoaded = false;

  @override
  void initState() {
    super.initState();

    _createInterstitialAd(); 

    _nativeAd = NativeAd(
      adUnitId: 'ca-app-pub-3940256099942544/2247696110',
      request: AdRequest(),
      factoryId: 'adFactoryExample',
      listener: NativeAdListener(
        onAdLoaded: (Ad ad) {
          print('natty $NativeAd loaded.');
          setState(() {
            _nativeAdIsLoaded = true;
          });
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('natty $NativeAd failedToLoad: $error');
          ad.dispose();
        },
      ),
    );
    _nativeAd.load();
  }

  Widget nAdWidget() {
    final AdWidget adWidget = AdWidget(ad: _nativeAd);
    return Builder(
      builder: (context) => Container(
        child: adWidget,
        width: 250,
        height: 350,
        alignment: Alignment.center,
      ),
    );
  }

>>Scaffold
(
Container(.....),
...
nAdWidget(),
....,
Widget(....),
)

@override
  void dispose() {

    _interstitialAd?.dispose();

    _nativeAd?.dispose(); 

    super.dispose();

  }

another page… Using inline native… same error…

No issues in Flutter doctor…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
brightchipcommented, Apr 4, 2022

I have the same error @viniciusddrft viniciusddrft
Can you help me fix this out?

PlatformException(NativeAdError, Can't find NativeAdFactory with id: adFactoryExample, null, null) I’m using Getx and this is my codes:

controller;

class NativeAdsController extends GetxController {
  late NativeAd myNativeAds;
  late AdWidget adWidget;

  final _loading = 0.obs;
  set loading(value) => this._loading.value = value;
  get loading => this._loading.value;

  NativeAdsController() {
    this.loadNativeAds();
  }

  get nativeAd {
    return this.myNativeAds;
  }

  get nativeAdWidget {
    return this.adWidget;
  }

  onClose() {
    this.disposeNativeAds();
  }

  loadNativeAds() async {
    myNativeAds = NativeAd(
      adUnitId: AdsHelper.getNativeAdUnitId(),
      factoryId: 'adFactoryExample',
      request: AdRequest(),
      listener: NativeAdListener(),
    );

    try {
      // this.disposeNativeAds();
      await myNativeAds.load();

      adWidget = AdWidget(ad: myNativeAds);

      this.loading++;
    } catch (err) {
      print('error load NativeAd ${err.toString()}');
    }
  }

  disposeNativeAds() {
    // adWidget.dispose();
    myNativeAds.dispose();
  }
}

widget;

class NativeAds extends StatelessWidget with WidgetThemes {
  NativeAdsController controller = Get.put(NativeAdsController());

  @override
  Widget build(BuildContext context) {
    initWidgetThemes(context);

    return Obx(
      () => ((controller.loading > 0) && (null != controller.nativeAd))
          ? Container(
              color: Colors.amber,
              alignment: Alignment.center,
              child: controller.nativeAdWidget,
              width: 200,
              height: 200,
            )
          : SizedBox.shrink(),
    );
  }
}

Setups in Android are:

    @Override
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "adFactoryExample", NativeAdFactoryExample( LayoutInflater.from(context)))
    }

    @Override
    override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
        super.cleanUpFlutterEngine(flutterEngine)
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample")
    }
}
class NativeAdFactoryExample(val layoutInflater: LayoutInflater) : NativeAdFactory {
    override fun createNativeAd(
        nativeAd: NativeAd, 
        customOptions: Map<String, Any>?
        ): NativeAdView  {
            val adView = layoutInflater.inflate(R.layout.my_native_ad, null) as NativeAdView 
            val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
            val bodyView = adView.findViewById<TextView>(R.id.ad_body)
            headlineView.text = nativeAd.headline
            bodyView.text = nativeAd.body
            adView.setBackgroundColor(Color.BLUE)
            adView.setNativeAd(nativeAd)
            adView.bodyView = bodyView
            adView.headlineView = headlineView
        return adView
    }
}```
0reactions
Thomas22986commented, Aug 10, 2022

same problem ad cant be loaded

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ad with the following id could not be found. Natiev Ad Flutter ...
the native ad is me show a an error. I use the Test ad id from google. "This ad may have not been...
Read more >
Adding an AdMob banner and native inline ads to a Flutter app
This codelab guides you through implementing an AdMob inline banner and AdMob native inline ads in a Flutter app using the Google Mobile...
Read more >
google_mobile_ads 0.13.6 | Flutter Package - Pub.dev
The Google Mobile Ads SDK for Flutter currently supports loading and displaying banner, interstitial (full-screen), native ads, and rewarded video ads.
Read more >
Easy-to-make Native Ads in Flutter - Morioh
Failure to do so can lead to suspension of your account. The easiest way to load test ads is to use the dedicated...
Read more >
App Open Ads | Flutter - Google Developers
The easiest way to load test ads is to use our dedicated test ad unit ... They have been specially configured to return...
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