Guide to migrating to version 8

Version 8 of the Yandex Mobile Ads plugin for Flutter brings a variety of API updates to make integration easier and keep things consistent with our native SDKs.

Key changes

Renamed the MobileAds class to YandexAds.

We've renamed the following properties and methods:

SDK 7

SDK 8

MobileAds.setLocationConsent(bool)

YandexAds.setLocationTracking(bool)

MobileAds.setAgeRestrictedUser(bool)

YandexAds.setAgeRestricted(bool)

Ad request

The AdRequestConfiguration class has been removed. Use AdRequest to configure your request — this is where you set the ad unit ID (adUnitId).

Targeting parameters (age, contextQuery, contextTags, gender, location) have been moved from AdRequest into a separate AdTargeting class.

Ad request

SDK 7

final adRequestConfiguration = AdRequestConfiguration(
  adUnitId: 'your-ad-unit-id',
  age: 25,
  contextQuery: 'query',
  parameters: {'key': 'value'},
);

await adLoader.loadAd(
  adRequestConfiguration: adRequestConfiguration,
);

SDK 8

final adRequest = AdRequest(
  adUnitId: 'your-ad-unit-id',
  targeting: AdTargeting(
    age: 25,
    contextQuery: 'query',
  ),
  parameters: {'key': 'value'},
);

final ad = await adLoader.loadAd(adRequest: adRequest);

Fullscreen formats: interstitial, rewarded, and app open ads

We've updated how you initialize loaders and request ads: the loader constructor is now called synchronously, while the loadAd method returns a Future containing the pre-loaded ad.

The InterstitialAdLoadListener, RewardedAdLoadListener, and AppOpenAdLoadListener classes have been removed. Use the loadAd result and standard error handling instead of callbacks.

Loading ads

SDK 7

Setting up loaders asynchronously using callbacks:

late final Future<InterstitialAdLoader> _adLoader =
      _createInterstitialAdLoader();
InterstitialAd? _ad;

Future<InterstitialAdLoader> _createInterstitialAdLoader() {
  return InterstitialAdLoader.create(
    onAdLoaded: (InterstitialAd interstitialAd) {
      // The ad was loaded successfully. Now you can show loaded ad
      _ad = interstitialAd;
    },
    onAdFailedToLoad: (error) {
      // Ad failed to load with AdRequestError.
      // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
    },
  );
}

Future<void> _loadInterstitialAd() async {
  final adLoader = await _adLoader;
  await adLoader.loadAd(adRequestConfiguration: AdRequestConfiguration(adUnitId: 'your-ad-unit-id'));
}

SDK 8

Synchronous loader initialization; loadAd returns the ad on success and throws an AdRequestError on failure:

final _adLoader = InterstitialAdLoader();
InterstitialAd? _ad;

Future<void> _loadInterstitialAd() async {
  try {
    _ad = await _adLoader.loadAd(
      adRequest: AdRequest(adUnitId: 'your-ad-unit-id'),
    );
  } on AdRequestError catch (error) {
    // Ad failed to load with AdRequestError.
    // Attempting to load a new ad from the error handler is strongly discouraged.
  }
}

The same rules apply to RewardedAdLoader and AppOpenAdLoader.

Banners

We've updated how you create and load banners: the constructor no longer accepts AdRequest or event handlers. You can now load ads via a separate load(AdRequest) call. To track loading progress and show events, subscribe to the loadStateStream and events streams.

Creating and loading a banner

SDK 7

BannerAd? _banner;

void _createBanner() {
  _banner = BannerAd(
    adSize: BannerAdSize.stickySize(width),
    adRequest: AdRequest(),
    onAdLoaded: () {
      // Ad loaded
    },
    onAdFailedToLoad: (error) {
      // Ad failed to load
    },
    onAdClicked: () {},
    onImpression: (data) {},
  );
}

SDK 8

BannerAd? _banner;

void _createBanner() {
  final banner = BannerAd(adSize: BannerAdSize.stickySize(width));

  banner.loadStateStream.listen((state) {
    if (state is BannerAdLoadStateLoaded) {
      // Ad loaded
    } else if (state is BannerAdLoadStateError) {
      // Ad failed to load
    }
  });

  banner.events.listen((event) {
    if (event is BannerAdClickedEvent) {
      // Ad clicked
    } else if (event is BannerAdImpressionEvent) {
      // Impression
    }
  });

  banner.load(AdRequest(adUnitId: 'your-ad-unit-id'));
  _banner = banner;
}

Destroying a banner

SDK 7

_banner?.destroy();

SDK 8

await _banner?.destroy();

AdInfo class

We've updated the structure of the AdInfo class: removed the adSize field and added the creatives, extraData, and partnerText fields.

SDK 7

SDK 8

AdInfo.adSize

Removed

AdInfo.creatives (List<Creative>)

AdInfo.extraData (String?)

AdInfo.partnerText (String?)

Deleted events

The onLeftApplication, onReturnedToApplication, and onAdClose events are no longer supported. You need to remove the corresponding handlers from your code.