-
-
Notifications
You must be signed in to change notification settings - Fork 58
Creating a rewarded ad
Rewarded ads are ads that users have the option of interacting with in exchange for in-app rewards.
To create a Rewarded Ad, use the class RewardedAd
:
RewardedAd rewardedAd = RewardedAd();
A single RewardedAd
object can be used to request and display multiple interstitial ads, so you only need
to construct it once.
In order to show an ad, it needs to be loaded first. You can use load()
to load the ad:
rewardedAd.load();
To verify if the ad is loaded, use rewardedAd.isLoaded
Before displaying a rewarded ad to users, you must present the user with an explicit choice to view rewarded ad content in exchange for a reward. Rewarded ads must always be an opt-in experience.
To show a RewardedAd
, use the isLoaded
getter to verify that it's done loading, then call show()
. The rewarded ad from the previous code example could be shown in a button's onPressed like this:
FlatButton(
child: Text('Open rewarded ad'),
onPressed: () async {
// Load only if not loaded
(!rewardedAd.isLoaded) await rewardedAd.load();
(rewardedAd.isLoaded) rewardedAd.show();
// Load the ad again after it's shown
rewardedAd.load();
},
),
To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events using rewarded.onEvent.listen((_) {...})
:
rewardedAd.onEvent.listen((e) {
final event = e.keys.first;
switch (event) {
case RewardedAdEvent.loading:
print('loading');
break;
case RewardedAdEvent.loaded:
print('loaded');
break;
case RewardedAdEvent.loadFailed:
final errorCode = e.values.first;
print('load failed $errorCode');
break;
case RewardedAdEvent.opened:
print('ad opened');
break;
case RewardedAdEvent.closed:
print('ad closed');
break;
case RewardedAdEvent.earnedReward:
final reward = e.values.first;
print('earned reward: $reward');
break;
case RewardedAdEvent.showFailed:
final errorCode = e.values.first;
print('show failed $errorCode');
break;
default:
break;
}
});
RewardedAdEvent.closed
is a handy place to load a new interstitial after displaying the previous one:
rewardedAd.onEvent.listen((e) {
final event = e.keys.first;
switch (event) {
case RewardedAdEvent.closed:
rewardedAd = RewardedAd.load();
break;
default:
break;
}
});
You can find a full example here