fetchAd method

void fetchAd()

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

Implementation

void fetchAd() {
  _log('Preparing to fetch...');

  if (_isLoading == true) {
    _log('Already fetching, early abort');
  } else if (kIsWeb == true) {
    _completer
        .complete(ResponseInterstitial(StatusInterstitial.notLoadedOnWeb));
    _log('Ads not available on the web, early abort');
  } else {
    _completer = Completer<ResponseInterstitial>();
    _isShown = false;
    _adToShow = null;

    _log('Fetching ...');
    _isLoading = true;

    InterstitialAd.load(
      adUnitId: _adUnitId,
      request: const AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (ad) {
          _isLoading = false;
          _adToShow = ad;
          _log('Loaded');

          ad.fullScreenContentCallback =
              FullScreenContentCallback(onAdShowedFullScreenContent: (ad) {
            _log('  - onAdShowedFullScreenContent');
          }, onAdImpression: (ad) {
            _log('  - onAdImpression');
          }, onAdFailedToShowFullScreenContent: (ad, err) {
            ad.dispose();
            _log(
                '  - onAdFailedToShowFullScreenContent, completing: displayFailedUnspecificReasons');
            _completer.complete(
                ResponseInterstitial(StatusInterstitial.notLoadedGenerally));
          }, onAdDismissedFullScreenContent: (ad) {
            ad.dispose();

            if (_isShown = true) {
              _log(
                  '  - onAdDismissedFullScreenContent, completing with: displaySuccess');
              _completer.complete(
                  ResponseInterstitial(StatusInterstitial.displaySuccess));
            } else {
              _log(
                  '  - onAdDismissedFullScreenContent, completing with: displayFailedUnspecificReasons');
              _completer.complete(ResponseInterstitial(
                  StatusInterstitial.displayFailedUnspecificReasons));
            }
          }, onAdClicked: (ad) {
            _log('  - onAdClicked');
          });
        },
        onAdFailedToLoad: (error) {
          _isLoading = false;
          _log(
              'Failed to load: ${error.code} ${error.message}, completing: notLoadedGenerally');
          _completer.complete(ResponseInterstitial(
              StatusInterstitial.notLoadedGenerally,
              admobErrorCode: error.code,
              admobErrorMessage: error.message));
        },
      ),
    );
  }
}