Interstitial Ads

Loading an Interstitial Ad

The following code shows you how to attach listeners and load the first interstitial:

final String _interstitial_ad_unit_ID = Platform.isAndroid ? "android_inter_ad_unit_ID" : "ios_inter_ad_unit_ID";

var _interstitialRetryAttempt = 0;

void initializeInterstitialAds() {

  AppLovinMAX.setInterstitialListener(InterstitialListener(
    onAdLoadedCallback: (ad) {
      // Interstitial ad is ready to show. AppLovinMAX.isInterstitialReady(_interstitial_ad_unit_ID) now returns 'true'.
      print('Interstitial ad loaded from ' + ad.networkName);

      // Reset retry attempt
      _interstitialRetryAttempt = 0;
    },
    onAdLoadFailedCallback: (adUnitId, error) {
      // Interstitial ad failed to load.
      // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds).
      _interstitialRetryAttempt = _interstitialRetryAttempt + 1;

      int retryDelay = pow(2, min(6, _interstitialRetryAttempt)).toInt();

      print('Interstitial ad failed to load with code ' + error.code.toString() + ' - retrying in ' + retryDelay.toString() + 's');

      Future.delayed(Duration(milliseconds: retryDelay * 1000), () {
        AppLovinMAX.loadInterstitial(_interstitial_ad_unit_ID);
      });
    },
    onAdDisplayedCallback: (ad) {
      ⋮
    },
    onAdDisplayFailedCallback: (ad, error) {
      ⋮
    },
    onAdClickedCallback: (ad) {
      ⋮
    },
    onAdHiddenCallback: (ad) {
      ⋮
    },
  ));

  // Load the first interstitial.
  AppLovinMAX.loadInterstitial(_interstitial_ad_unit_ID);
}

Showing an Interstitial Ad

To show an interstitial ad, call showInterstitial():

bool isReady = (await AppLovinMAX.isInterstitialReady(_interstitial_ad_unit_ID))!;
if (isReady) {
  AppLovinMAX.showInterstitial(_interstitial_ad_unit_ID);
}