I am trying to call an ad dynamically in a listview
but throws this error :
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.
Here is the code
Import package google_mobile_ads
import 'package:google_mobile_ads/google_mobile_ads.dart';
Then instantiated the package
NativeAd _nativeAd;
final Completer<NativeAd> nativeAdCompleter = Completer<NativeAd>();
I function to load the Ads
loadAd(){
_nativeAd = NativeAd(
adUnitId: "ca-app-pub-3940256099942544/1044960115",
request: AdRequest(),
factoryId: 'adFactoryExample',
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$NativeAd loaded.');
nativeAdCompleter.complete(ad as NativeAd);
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
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());
}
Then to show the add I did this in a switch case statement
case 'ad':
loadAd();
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 Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
margin: EdgeInsets.only(top: 100, left: 5, right: 5, bottom: 70),
child: Center(child: child),
color: Colors.black,
),
);
},
);
break;
This integration throws the above exception while showing ads from the second ad