So i came up against this and could not get it to work until i found this https://github.com/googleads/googleads-mobile-flutter/issues/399 so making your ad widget use the auto keep alive mixin apparently helps, i tried that with my code and it would not work i then used the example in the link which did work, the only difference i can see is he is creating an instance of the ad in init state and then calling .load() on that instance, where as i was using the dart notation and creating the instance and calling ..load() in the same call Object()..load() i think these things are usually identical but i switched to his version and it worked. Below is the example the only i thing i swapped was the style from using the factoryId to specifying a style
class NativeInlineAd extends StatefulWidget {
const NativeInlineAd();
@override
State createState() => _NativeInlineAdState();
}
class _NativeInlineAdState extends State<NativeInlineAd>
with AutomaticKeepAliveClientMixin {
// COMPLETE: Add NativeAd instance
late NativeAd _ad;
// COMPLETE: Add _isAdLoaded
bool _isAdLoaded = false;
@override
void initState() {
super.initState();
// COMPLETE: Create a NativeAd instance
_ad = NativeAd(
adUnitId: kReleaseMode
? '<YOUR_NATIVE_ADMOB_ID>'
: '<NATIVE_TEST_AD_ID>',
factoryId: 'googleNativeAdsCard',
request: const AdRequest(),
listener: NativeAdListener(
onAdLoaded: (_) {
setState(() {
_isAdLoaded = true;
});
},
onAdFailedToLoad: (ad, error) {
// Releases an ad resource when it fails to load
ad.dispose();
throw 'Ad load failed (code=${error.code} message=${error.message})';
},
),
);
// COMPLETE: Load an ad
_ad.load();
}
@override
Widget build(BuildContext context) {
super.build(context);
if (_isAdLoaded) {
return Container(
child: SizedBox(height: 200, child: AdWidget(ad: _ad)),
),
height: 270,
margin: const EdgeInsets.only(
left: 10,
right: 10,
top: 5,
),
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
ValuesConstant.borderRadius,
),
border: Border.all(
width: 1,
color: Colors.black,
),
color: Theme.of(context).cardTheme.color,
),
alignment: Alignment.center,
);
}
return const SizedBox(
height: 72.0,
child: Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
Colors.grey,
),
),
),
);
}
@override
void dispose() {
_ad.dispose();
super.dispose();
}
@override
bool get wantKeepAlive => true;
}