This AdWidget is already in the Widget tree while using native ad

Viewed 50

I am using google_mobile_ads package to show Native ad in my app, everything is working fine, but i want to show native ad multiple time in list and thus application throw the exception.

Below is my code what i had tried so far.

class _NativeAddScreenState extends State<NativeAddScreen> {
   late NativeAd nativeAd;
   bool isAdLoaded = false;
 

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    nativeAd = NativeAd(
    factoryId: "listTile",
    adUnitId: NativeAd.testAdUnitId,
    request: const AdRequest(),
      listener: NativeAdListener(
        onAdLoaded: (_){
          print('Hey i have loaded the Native ad');
          setState(() {
            isAdLoaded = true;
          });
        },
        onAdFailedToLoad: (ad,error){
          ad.dispose();
          print('This is error while loading $error');
        }
      )
    );
    nativeAd.load();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: isAdLoaded ? Container(
            child: ListView.builder(
              itemCount: 100,
                itemBuilder: (context,index){
                  return ListTile(
                    onTap: (){
                    },
                    title: index % 10 == 0 ? SizedBox(
                        width: MediaQuery.of(context).size.width,
                        height: 50,
                        child: bannerAdWidget(nativeAd)): Text('Hello') ,
                  );
                }),
          ) : const Center(child: CircularProgressIndicator(
            color: Colors.blue,
          ))
          ),
    );
  }

  Widget bannerAdWidget(var _nativeAd) {
    return StatefulBuilder(
      builder: (context, setState) => AdWidget(ad: _nativeAd),
    );
  }
}

I followed all the previous question asked and try all suggested solutions on this platform but none of them is working for me.

I also tried the below solution suggested on Github but it shows the following message on screen

Error Message

0 Answers
Related