loadRewardedVideoAd method

dynamic loadRewardedVideoAd(
  1. Function callback
)

Implementation

loadRewardedVideoAd(Function callback) async {
  if (admodel?.issdkads ?? false) {
    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();
            loadRewardedVideoAd(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?.fBrewardedAd ?? '',
          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) {
              loadRewardedVideoAd(callback);
              debugPrint('RewardedAd failed to load: $error');
            },
          ));
    }
  } else {
    await PluginCodelabPlatform.instance.showreward();
  }
}