loadFBRewardedVideoAd method

dynamic loadFBRewardedVideoAd(
  1. Function callback
)

Implementation

loadFBRewardedVideoAd(Function callback) async {
  if (admodel?.isfacebook ?? false) {
    FacebookRewardedVideoAd.loadRewardedVideoAd(
      placementId: admodel?.fbrewardedAd ?? '',
      listener: (result, value) {
        print("Rewarded Ad: $result --> $value");
        if (result == RewardedVideoAdResult.LOADED) {
          Future.delayed(const Duration(seconds: 1), () {
            FacebookRewardedVideoAd.showRewardedVideoAd();
          });
        }
        if (result == RewardedVideoAdResult.ERROR) {
          FacebookRewardedVideoAd.destroyRewardedVideoAd();
          loadFBRewardedVideoAd(callback);
        }

        /// Once a Rewarded Ad has been closed and becomes invalidated,
        /// load a fresh Ad by calling this function.
        if (result == RewardedVideoAdResult.VIDEO_CLOSED &&
            (value == true || value["invalidated"] == true)) {}
      },
    );
  } else {
    RewardedAd? _rewardedAd;
    RewardedAd.load(
      adUnitId: admodel?.admobrewardedAd ?? '',
      request: const AdRequest(),
      rewardedAdLoadCallback: RewardedAdLoadCallback(
        // Called when an ad is successfully received.
        onAdLoaded: (ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
            // Called when the ad showed the full screen content.
            onAdShowedFullScreenContent: (ad) {},
            // Called when an impression occurs on the ad.
            onAdImpression: (ad) {},
            // Called when the ad failed to show full screen content.
            onAdFailedToShowFullScreenContent: (ad, err) {
              // Dispose the ad here to free resources.
              ad.dispose();
              callback.call();
            },
            // Called when the ad dismissed full screen content.
            onAdDismissedFullScreenContent: (ad) {
              // Dispose the ad here to free resources.
              ad.dispose();
              _rewardedAd!.show(
                onUserEarnedReward: (
                  AdWithoutView ad,
                  RewardItem rewardItem,
                ) {
                  // Reward the user for watching an ad.
                },
              );
            },
            // Called when a click is recorded for an ad.
            onAdClicked: (ad) {},
          );

          debugPrint('$ad loaded.');
          // Keep a reference to the ad so you can show it later.
          _rewardedAd = ad;
        },
        // Called when an ad request failed.
        onAdFailedToLoad: (LoadAdError error) {
          loadFBRewardedVideoAd(callback);
          debugPrint('RewardedAd failed to load: $error');
        },
      ),
    );
  }
}