5

To show the ad I do this:

await adState.showRewardedAd();
await adState.rewardedAd!.show(onUserEarnedReward: (RewardedAd ad, RewardItem reward) {
// reward
});

and:

Future createRewardedAd([bool showVideo = false]) async {
    await RewardedAd.load(
      adUnitId: rewardeUnitAdId,
      request: request,
      rewardedAdLoadCallback: RewardedAdLoadCallback(
        onAdLoaded: (RewardedAd ad) async {
          adState.rewardedAd = ad;
          _numRewardedLoadAttempts = 0;
          if (showVideo) {
            showRewardedAd();
          }
        },
        onAdFailedToLoad: (LoadAdError error) async {
          adState.rewardedAd = null;
          _numRewardedLoadAttempts += 1;
          if (_numRewardedLoadAttempts <= maxFailedLoadAttempts) {
            await createRewardedAd();
          }
        },
      ),
    );
  }

  Future<void> showRewardedAd() async {
    if (adState.rewardedAd == null) {
      return await createRewardedAd(true);
    }

    adState.rewardedAd!.fullScreenContentCallback =
        await FullScreenContentCallback(
      onAdShowedFullScreenContent: (RewardedAd ad) => {},
      onAdDismissedFullScreenContent: (RewardedAd ad) async {
        ad.dispose();
        await createRewardedAd();
      },
      onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) async {
        ad.dispose();
        await createRewardedAd();
      },
    );
  }

So every time we close the rewardAd we create/load it again to have it prepared for next time. Until here everything is fine.


The problem comes when we click to show it again too fast. When we do:

if (adState.rewardedAd == null) {
      return await createRewardedAd(true);
    }

I'm forcing it to be loaded and I wait for await RewardedAd.load() BUT despite I wait for that, the onAdLoaded event takes some time and the async function doesn't know it's not ready yet.

So, when I do await adState.rewardedAd! it can be null.

How could I make this more secure? Google's documentation is not covering this

Dani
  • 3,128
  • 2
  • 43
  • 91

0 Answers0