showRewardedAd method
Future<void>
showRewardedAd(
- BuildContext context, {
- void onAdShowedFullScreenContent()?,
- void onAdDismissedFullScreenContent()?,
- void onAdFailedToShowFullScreenContent()?,
- void onUserEarnedReward()?,
<------------------------ Show Rewarded Ad ------------------------>
Implementation
// Function to show a rewarded ad.
Future<void> showRewardedAd(
BuildContext context, {
void Function(RewardedAd)? onAdShowedFullScreenContent,
void Function(RewardedAd)? onAdDismissedFullScreenContent,
void Function(RewardedAd, AdError)? onAdFailedToShowFullScreenContent,
void Function(AdWithoutView, RewardItem)? onUserEarnedReward,
}) async {
// Check if the rewarded ad is loaded
if (rewardedAd == null) {
if (!context.mounted) return; // Return if the context is not mounted
createRewardedAd(context); // Create the rewarded ad
return;
}
// Set callbacks and show the rewarded ad
rewardedAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: onAdShowedFullScreenContent,
// Set callback for when ad is shown
onAdDismissedFullScreenContent: (RewardedAd ad) {
// Call the callback function when ad is dismissed
if (onAdDismissedFullScreenContent != null) {
onAdDismissedFullScreenContent(ad);
}
rewardedAd == null; // Clear the reference to the ad
ad.dispose(); // Dispose the ad object
if (!context.mounted) return; // Return if the context is not mounted
createRewardedAd(context); // Create a new rewarded ad
},
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
// Call the callback function when ad fails to show
if (onAdFailedToShowFullScreenContent != null) {
onAdFailedToShowFullScreenContent(ad, error);
}
ad.dispose(); // Dispose the ad object
},
);
// Show the rewarded ad
await rewardedAd!.show(
onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
if (onUserEarnedReward != null) {
onUserEarnedReward(ad, reward);
}
// User earned a reward.
log('Earn with reward $RewardItem(${reward.amount}, ${reward.type})');
},
);
}