fetchAd method

void fetchAd()

Fetches an ad in the background. No neet to await for it.

Implementation

void fetchAd() {
  _completer = Completer<ResponseInterstitial>();

  if (kIsWeb == true) {
    _completer
        .complete(ResponseInterstitial(StatusInterstitial.notLoadedOnWeb));
    _log('Aborted loading InterstitialAd: ads not available on the web');
  } else {
    _log('Loading InterstitialAd');

    InterstitialAd.load(
      adUnitId: _adUnitId,
      request: const AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
              // Called when the ad showed the full screen content.
              onAdShowedFullScreenContent: (ad) {
                // success case is handled in showAd
              },
              // Called when an impression occurs on the ad.
              onAdImpression: (ad) {},
              // Called when the ad failed to show full screen content.
              onAdFailedToShowFullScreenContent: (ad, err) {
                ad.dispose();
                _completer.complete(ResponseInterstitial(
                    StatusInterstitial.notLoadedGenerally));
              },
              // Called when the ad dismissed full screen content.
              onAdDismissedFullScreenContent: (ad) {
                ad.dispose();
              },
              // Called when a click is recorded for an ad.
              onAdClicked: (ad) {});

          _adToShow = ad;
          _log('InterstitialAd loaded');
        },
        onAdFailedToLoad: (error) {
          _log(
              'InterstitialAd failed to load: ${error.code} ${error.message}');
          _completer.complete(ResponseInterstitial(
              StatusInterstitial.notLoadedGenerally,
              admobErrorCode: error.code,
              admobErrorMessage: error.message));
        },
      ),
    );
  }
}