showAppOpenAdIfAvailable method
void
showAppOpenAdIfAvailable()
Shows the ad, if one exists and is not already being shown.
If the previously cached ad has expired, this just loads and caches a new ad.
Implementation
void showAppOpenAdIfAvailable() {
if (!enableAd) {
return;
}
if (!isAppOpenAdAvailable) {
debugPrint('Tried to show ad before available.');
_loadAppOpenAd();
return;
}
if (_isShowingAppOpenAd) {
debugPrint('Tried to show ad while already showing an ad.');
return;
}
if (DateTime.now().subtract(maxCacheDuration).isAfter(_appOpenLoadTime!)) {
debugPrint('Maximum cache duration exceeded. Loading another ad.');
_appOpenAd!.dispose();
_appOpenAd = null;
_loadAppOpenAd();
return;
}
// Set the fullScreenContentCallback and show the ad.
_appOpenAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (ad) {
_isShowingAppOpenAd = true;
debugPrint('$ad onAdShowedFullScreenContent');
},
onAdFailedToShowFullScreenContent: (ad, error) {
debugPrint('$ad onAdFailedToShowFullScreenContent: $error');
_isShowingAppOpenAd = false;
ad.dispose();
_appOpenAd = null;
_loadAppOpenAd();
},
onAdDismissedFullScreenContent: (ad) {
debugPrint('$ad onAdDismissedFullScreenContent');
_isShowingAppOpenAd = false;
ad.dispose();
_appOpenAd = null;
_loadAppOpenAd();
},
);
_appOpenAd!.show();
}