fetchAd method

void fetchAd()

Fetches an ad in the background.

Implementation

void fetchAd() {
  _log('Checking fetching RewardedInterstitialAd ...');

  _completer = Completer<ResponseInterstitialRewarded>();
  _adToShow = null;

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

    RewardedInterstitialAd.load(
      adUnitId: _adUnitId,
      request: const AdRequest(),
      rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
        onAdLoaded: (ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
              // Called when the ad showed the full screen content.
              onAdShowedFullScreenContent: (ad) {
            // Succes handling is donw in:
            // _adToShow?.show( onUserEarnedReward
            //
            // Do NOT handle that here.
            _log('InterstitialRewardedAd onAdShowedFullScreenContent');
          },
              // Called when an impression occurs on the ad.
              onAdImpression: (ad) {
            _log('InterstitialRewardedAd onAdImpression');
          },
              // Called when the ad failed to show full screen content.
              onAdFailedToShowFullScreenContent: (ad, err) {
            ad.dispose();

            // Is this true:
            // Did the user cut the ad short?
            // @TODO

            _log('InterstitialRewardedAd onAdFailedToShowFullScreenContent');

            _completer.complete(ResponseInterstitialRewarded(
                StatusInterstitialRewarded.displayDeniedByUser));
          },
              // Called when the ad dismissed full screen content.
              onAdDismissedFullScreenContent: (ad) {
            _log('InterstitialRewardedAd onAdDismissedFullScreenContent');
            // This happens when onUserEarnedReward() had not been called,
            // which means the user dismissed the add.
            // Thus, it needs to get completed here.
            if (_completer.isCompleted == false) {
              _completer.complete(ResponseInterstitialRewarded(
                  StatusInterstitialRewarded.displayDeniedByUser));
            }

            ad.dispose();
          },
              // Called when a click is recorded for an ad.
              onAdClicked: (ad) {
            _log('InterstitialRewardedAd onAdClicked');
          });

          _adToShow = ad;
          _log('InterstitialRewardedAd loaded');
        },
        onAdFailedToLoad: (LoadAdError error) {
          _log(
              'RewardedInterstitialAd failed to load: ${error.code} ${error.message}');
          _completer.complete(ResponseInterstitialRewarded(
              StatusInterstitialRewarded.notLoadedGenerally,
              admobErrorCode: error.code,
              admobErrorMessage: error.message));
        },
      ),
    );
  }
}