showInterstitialAd method

void showInterstitialAd(
  1. BuildContext context, {
  2. void onAdShowedFullScreenContent(
    1. InterstitialAd
    )?,
  3. void onAdDismissedFullScreenContent(
    1. InterstitialAd
    )?,
  4. void onAdFailedToShowFullScreenContent(
    1. InterstitialAd,
    2. AdError
    )?,
})

Displays the loaded interstitial ad.

Implementation

void showInterstitialAd(
  BuildContext context, {
  void Function(InterstitialAd)? onAdShowedFullScreenContent,
  void Function(InterstitialAd)? onAdDismissedFullScreenContent,
  void Function(InterstitialAd, AdError)? onAdFailedToShowFullScreenContent,
}) {
  // Check if the interstitial ad is loaded
  if (interstitialAd == null) {
    // If ad is not loaded, create a new one
    if (!context.mounted) return; // Return if the context is not mounted
    createInterstitialAd(context); // Create the interstitial ad
    return;
  }

  // Set callbacks and show the interstitial ad
  interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
    onAdShowedFullScreenContent: onAdShowedFullScreenContent,
    onAdDismissedFullScreenContent: (InterstitialAd ad) {
      // Call the callback function when ad is dismissed
      if (onAdDismissedFullScreenContent != null) {
        onAdDismissedFullScreenContent(ad);
      }
      debugPrint('$ad onAdDismissedFullScreenContent.');
      interstitialAd = null; // Set ad to null after it's dismissed
      ad.dispose(); // Dispose the ad object
      if (!context.mounted) return; // Return if the context is not mounted
      createInterstitialAd(context); // Create a new interstitial ad
    },
    onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
      // Call the callback function when ad fails to show
      if (onAdFailedToShowFullScreenContent != null) {
        onAdFailedToShowFullScreenContent(ad, error);
      }
      debugPrint('$ad onAdFailedToShowFullScreenContent: $error');
      ad.dispose(); // Dispose the ad object
    },
  );

  interstitialAd!.show(); // Show the interstitial ad
}