Rewarded ads are ads that users have the option of interacting with in exchange for in-app rewards. This guide shows how to integrate rewarded ads from Ad Manager into a Flutter app.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for rewarded ads:
/21775744923/example/rewarded
The test ad units are configured to return test ads for every request, and you're free to use them in your own apps while coding, testing, and debugging. Just make sure you replace them with your own ad unit IDs before publishing your app.
Load an ad
The following example loads a rewarded ad:
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/21775744923/example/rewarded'; /// Loads a rewarded ad. void loadAd() { RewardedAd.loadWithAdManagerAdRequest( adUnitId: adUnitId, adManagerAdRequest: const AdManagerAdRequest(), adLoadCallback: RewardedAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { debugPrint('$ad loaded.'); // Keep a reference to the ad so you can show it later. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('RewardedAd failed to load: $error'); }, )); } }
Rewarded ad events
Through the use of FullScreenContentCallback
, you can listen for lifecycle
events, such as when the ad is shown or dismissed. Set
RewardedAd.fullScreenContentCallback
before showing the ad to receive
notifications for these events. This example implements each method and logs a
message to the console:
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/21775744923/example/rewarded'; /// Loads a rewarded ad. void loadAd() { RewardedAd.loadWithAdManagerAdRequest( adUnitId: adUnitId, adManagerAdRequest: const AdManagerAdRequest(), adLoadCallback: RewardedAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { ad.fullScreenContentCallback = FullScreenContentCallback( // Called when the ad showed the full screen content. onAdShowedFullScreenContent: (ad) {}, // Called when an impression occurs on the ad. onAdImpression: (ad) {}, // Called when the ad failed to show full screen content. onAdFailedToShowFullScreenContent: (ad, err) { // Dispose the ad here to free resources. ad.dispose(); }, // Called when the ad dismissed full screen content. onAdDismissedFullScreenContent: (ad) { // Dispose the ad here to free resources. ad.dispose(); }, // Called when a click is recorded for an ad. onAdClicked: (ad) {}); debugPrint('$ad loaded.'); // Keep a reference to the ad so you can show it later. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('RewardedAd failed to load: $error'); }, )); } }
Display ad
A RewardedAd
is displayed as an Overlay on top of all app content and is
statically placed; thus, it can't be added to the Flutter widget tree. You can
choose when to show the ad by calling show()
.
RewardedAd.show()
takes an OnUserEarnedRewardCallback
, which is invoked when
the user earns a reward. Be sure to implement this and reward the user for
watching an ad.
_rewardedAd.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) { // Reward the user for watching an ad. });
Once show()
is called, an Ad
displayed this way can't be removed
programmatically and requires user input. A RewardedAd
can only be shown
once. Subsequent calls to show will trigger onAdFailedToShowFullScreenContent
.
An ad must be disposed when access to it is no longer needed. The best practice
for when to call dispose()
is in the
FullScreenContentCallback.onAdDismissedFullScreenContent
and
FullScreenContentCallback.onAdFailedToShowFullScreenContent
callbacks.
That's it! Your app is now ready to display rewarded ads.