Skip to content
This repository has been archived by the owner on Mar 12, 2022. It is now read-only.

Creating a rewarded ad

Bruno D'Luka edited this page Feb 14, 2021 · 5 revisions

Rewarded ad

Rewarded ads are ads that users have the option of interacting with in exchange for in-app rewards.

Create a Rewarded Ad

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.

Load the ad

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

Show the ad

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();
  },
),

Listening to events

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;
  }
});

Using the listener to reload

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;
  }
});

Do NOT show a new ad in RewardedAdEvent.closed, because it'll become infinite.

You can find a full example here