Enabling rewarded ads
This is an archived version of the documentation. Actual documentation for all platforms can be found here.
A rewarded ad is a configurable full-screen ad. The user gets a reward for viewing the ad.
Creating and displaying RewardedAd
Add the import:
import 'package:yandex_mobileads/mobile_ads.dart';
Copied to clipboardCreate a
RewardedAd
class object. You can only use the asynchronous method to create the object:final ad = await RewardedAd.create( adUnitId: 'R-M-XXXXXX-Y', onAdLoaded: () { /* Do something */ }, onAdFailedToLoad: (error) { /* Do something */ }, );
Copied to clipboardAfter you create the
RewardedAd
class object, load the ads. To load an ad, use theload
method that accepts an optionalAdRequest
object.await ad.load(adRequest: AdRequest());
Copied to clipboardYou can wait for your ad to load asynchronously using the
await
keyword.Rewarded ads are loaded in the background immediately after the
load
method is called. To display an ad, call theshow
method:await ad.show();
Copied to clipboardYou can wait for the ad to start displaying asynchronously.
Alternatively, you can use the
waitForDismiss
asynchronous method to wait until the end of the ad serving and/or reward:Reward? reward = await ad.waitForDismiss();
Copied to clipboard
Example of working with rewarded ads
The code demonstrates how to create and configure a RewardedAd
object, and how to load and display a rewarded ad:
Future<void> showRewardedAd() async { final ad = await RewardedAd.create( adUnitId: 'demo-rewarded-yandex', onAdLoaded: () { /* Do something */ }, onAdFailedToLoad: (error) { /* Do something */ }, ); await ad.load(adRequest: AdRequest()); await ad.show(); final reward = await ad.waitForDismiss(); if (reward != null) debugPrint('got ${reward.amount} of ${reward.type}'); }
Copied to clipboard
If an ad is integrated this way, the ad unit appears when showRewardedAd
is called.
demo-rewarded-yandex