3

I'm trying to implement Ads in my App. Currently I am working on Google Ad Mobs Rewarded Video Ads. My Problem is, that after the first time I press the button which should load the ad an error becomes:

Unhandled Exception: PlatformException(ad_not_loaded, show failed for rewarded video, no ad was loaded, null)

is thrown, but if I press again it works properly. I have attached some relevant Code

Setting the targeting Info, loading the ad and creating a listener:

  @override
  void initState() {

    MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
        keywords: <String>['flutterio', 'beautiful apps'],
        contentUrl: 'https://flutter.io',
        childDirected: false,
    );

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

    RewardedVideoAd.instance.listener =
        (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
      if (event == RewardedVideoAdEvent.rewarded) {
        Navigator.push(
                      context,
                      this.route);
      }
      else if(event == RewardedVideoAdEvent.failedToLoad){
        Navigator.push(
                      context,
                      this.route);
      }
    };
    super.initState();
  }

The Button which starts the ad (it is located in the build method of the same class):

              GestureDetector(
                child: AdButton(theme.wopGH, "Gratis spielen"),
                onTap: () {
                  RewardedVideoAd.instance.show();
                },
              ),
Aksen P
  • 4,564
  • 3
  • 14
  • 27

1 Answers1

0

Your button should be enabled only if the ad is loaded. Before showing it, you must wait for it to load and it takes a few seconds. On initState, you should assign a state to your button, that way it will be disabled. And when the ad is loaded, you should setState to enable it.

if (event == RewardedVideoAdEvent.loaded) {
    ...
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68