Ad with the following id could not be found. Natiev Ad Flutter google_mobile_ads

Viewed 612

the native ad is me show a an error. I use the Test ad id from google.
"This ad may have not been loaded or has been disposed. Ad with the following id could not be found:1"

enter image description here

Can anyone help me?

EDIT #1 My Code There are a different example codes for native Ad? I use google_mobile_ads: ^0.13.2

...

   class _GameState extends State<Game> {
  static const AdRequest targetingInfo = AdRequest(
  );

  static const int maxFailedLoadAttempts = 3;



  static const _adUnitIDNative = "ca-app-pub-3940256099942544/2247696110";

  late StreamSubscription _subscription;
  bool? _deutsch;

  final NativeAd myNative = NativeAd(
    adUnitId: _adUnitIDNative,
    factoryId: 'listTile',
    request: AdRequest(),
    listener: NativeAdListener(),
  );

  final NativeAdListener listener = NativeAdListener(
    // Called when an ad is successfully received.
    onAdLoaded: (Ad ad) => print('Ad loaded.'),
    // Called when an ad request failed.
    onAdFailedToLoad: (Ad ad, LoadAdError error) {
      // Dispose the ad here to free resources.
      ad.dispose();
      print('NativeAd failed to load: $error');
    },
    // Called when an ad opens an overlay that covers the screen.
    onAdOpened: (Ad ad) => print('Ad opened.'),
    // Called when an ad removes an overlay that covers the screen.
    onAdClosed: (Ad ad) => print('Ad closed.'),
    // Called when an impression occurs on the ad.
    onAdImpression: (Ad ad) => print('Ad impression.'),
    // Called when a click is recorded for a NativeAd.
    onNativeAdClicked: (NativeAd ad) => print('Ad clicked.'),
  );

  get adContainer => null;

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

    _createInterstitialAd();
   
    myNative.load();
  }

...

Container(
                      alignment: Alignment.center,
                      child: AdWidget(ad: myNative),
                      padding: EdgeInsets.all(10),
                      margin: EdgeInsets.only(bottom: 20.0),
                      height: 70,                      
                    ),

Thanks

1 Answers

Your AdWidget is building before the ad has finished loading; you need to implement three things:

  1. Boolean Flag: to indicate the ad has finished loading;
  2. Listening event: on ad loaded, set flag to true and trigger rebuild; and
  3. Trigger Widget: on true, build the ad widget.
  // 1. Create bool
  bool isAdLoaded = false;

  // 2. Add listener event
  final NativeAdListener listener = NativeAdListener(
    // Called when an ad is successfully received.
    onAdLoaded: (Ad ad) => {
      setState(() {
        isAdLoaded = true;
      });

  ...

  // 3. Wrap the AdWidget inside a switch
  isAdLoaded ? Container(
    alignment: Alignment.center,
    child: AdWidget(ad: myNative),
    padding: EdgeInsets.all(10),
    margin: EdgeInsets.only(bottom: 20.0),
    height: 70,                      
  ) : CircularProgressIndicator(),
Related