0
 void initState() {
    // TODO: implement initState
    bannerAd = BannerAd(
        size: AdSize.banner,
        adUnitId: AdMobService.bannerAdUnitId,
        listener: BannerAdListener(
          onAdLoaded: (_) {
            setState(() {
              _bstatus = true;
            });
          },
          onAdFailedToLoad: (ad, err) {
            print("failed to load banner$err");
            ad.dispose();
          },
        ),
        request: new AdRequest());
    bannerAd.load();
  }  

// code where it causes error

 bottomNavigationBar: Container(
          height: 50,
          child: Stack(
            children:[
              Text("Ad  space"),
               if (_bstatus)
                AdWidget(
                ad: bannerAd,
                key: UniqueKey(),
              ),
              ]

          ))

This is my code _bstatus is to check if banner is loaded or not. On starting an app it works fine but when ads get reload it give the below error

The following assertion was thrown building AdWidget-[#06603](dirty, state: _AdWidgetState#c9e02):
This AdWidget is already in the Widget tree
If you placed this AdWidget in a list, make sure you create a new instance in the builder function
with a unique ad object.
Make sure you are not using the same ad object in more than one AdWidget.

But if I do hot reload it works fine

How could I resolve it?

3 Answers3

1

Those, who still facing the issue, should use the below code as a solution.

  1. Create a separate File.
  2. Just Call the widget Tile in your ListView and pass PubId (Provided by Admob) to it.

Make Sure to create a separate file for the code

class AdMobWidgetTile extends StatefulWidget {
          const AdMobWidgetTile({required this.adId, Key? key}) : super(key: key);
          final String adId;
          @override
          State<AdMobWidgetTile> createState() => _AdMobWidgetTileState();
        }
        
        class _AdMobWidgetTileState extends State<AdMobWidgetTile> {
          BannerAd? bAd;
          String id=UC.getRandom;//Any Random String
          @override
          void initState() {
            // TODO: implement initState
            super.initState();
            BannerAd(
                size: AdSize(
                  height: 250,
                  width: 320,
                ),
            
            adUnitId:widget.adId,
            listener: BannerAdListener(
                onAdClosed: (Ad ad) {},
                onAdFailedToLoad: (Ad ad, LoadAdError error) {
                  log("BANNER AD FAILED:");
                  ad.dispose();
                },
                onAdLoaded: (Ad ad) {
                  setState(() {
                    bAd =ad as BannerAd;
                  });
                  log("BANNER AD LOADED:");
                },
                onAdOpened: (Ad ad) {}),
            request: AdRequest())..load();
      }
    
    
    
      @override
      Widget build(BuildContext context) {
        return
          bAd!=null?
          Container(height: 250,
            margin: EdgeInsets.symmetric(vertical: 5,),
            child: AdWidget(
                  ad: bAd!,
                  key:Key(id) ,
                ),
          ):20.x;
      }
      @override
      void dispose() {
        // TODO: implement dispose
        super.dispose();
        bAd?.dispose();
      }
    }
Mehran Ullah
  • 550
  • 4
  • 17
0

This seem to be a duplicate of the following question:

Flutter :- This AdWidget is already in the Widget tree. How to disable this exception. And what does it mean?

The error is quite explicit, did you make sure to provide an unique ID ?

Colin Lazarini
  • 754
  • 4
  • 14
0

The problem is that you are putting the same widget again and again. You can fix this by creating a new stateful class and returning the Adwidget. This will build that same widget multiple time—it works like a Builder. This solved my problem; I hope it will work for you too.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Kafil khan
  • 81
  • 2
  • 5