PlatformException(ad_not_loaded, show failed for rewarded video, no ad was loaded, null) in flutter app

Viewed 425

I'm trying to show reward video ads in my flutter app. Sometimes it works perfectly fine and some other times I get this error "PlatformException(ad_not_loaded, show failed for rewarded video, no ad was loaded, null)"

Here is how I load and show ads:

  MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
    testDevices: ['6E85178A5E7B957403B497917D014D74'],
  );
  try {
      await RewardedVideoAd.instance.load(
          adUnitId:getRewardBasedVideoAdUnitId(), targetingInfo: targetingInfo);

      await RewardedVideoAd.instance.show();
    } on PlatformException catch (e) {

      Fluttertoast.showToast(
        msg: AppStrings.adFailed,
        toastLength: Toast.LENGTH_LONG,
        timeInSecForIosWeb: 5,
      );
    }

Usually this happens when ad hows successfully then the next time it fails and the time after it succeeds and so on, Maybe I need to reset something after ad shows successfully?

1 Answers

This is kinda coming late but I hope it helps someone out there.

The above condition usually happens because the load function returns before actually loading the ad.

This answer is a hack rather than a solution.

after initializing the plugin;

checkout the code below

showRewardAd() async {

  RewardedVideoAd.instance.load(
     adUnitId: RewardedVideoAd.testAdUnitId,
     targetingInfo: targetingInfo,
  );

  bool _adIsDisplayed = false;
  do {
    try {
      _adIsDisplayed = (await RewardedVideoAd.instance.show() ?? false);
    } catch (e) {}
  } while (!_adIsDisplayed);
 }
}

calling showRewardAd will forcefully show an ad. do not forget to add a loader to indicate progress while the ad loads.

we can leverage the fact that the show() methods of the RewardedVideoAd instance returns a Future<true> when successful and Future<false> otherwise.

using a do while loop will keep it trying to show the ad, till it succeeds, without throwing any error...

This may not be a wanted behavior so you might want to throw an error at some point during the loop, say after 5 - 10 tries, so we don't have an infinite loop.

Related