AndriousSolutions / ads

Other
58 stars 11 forks source link

Displaying reward video gets stuck when I click a couple of times #37

Closed Marek00Malik closed 3 years ago

Marek00Malik commented 3 years ago

Hi, I have this case when the user is extra hyperactive and clicks a couple of times the button that shows the ads. The end result, no ad gets loaded, and on following actions I'm getting

[Ads()] An Ads class is already instantiated!

The use case is that the user places on one screen and every 3th action he should see an ad to make more moves. The screen is a Statefull Widget so I'm not cleaning the widget or reinitializing it. Is that the case? Should I dispose of the Ads every time I finish showing the ad?

Andrious commented 3 years ago

Allow me to get back to you in a few days. Very busy right now. Look again at the example found with this Dart package for some guidance.

Marek00Malik commented 3 years ago

Thank you very much. I'm trying out the proposed solution you were talking about here But I have one problem, passing the RewardedVideoAdListener function body from were I'm executing the static class, to show the ad. Maybe that is the problem.

This is the code snippet:

 if (!AdManager.canUserSeeDish(userProvider.userInfo, sharedPreferences)) {
         AdManager.initAds();
         AdManager.showRewardAd(_rewardVideoListener);
}
....

_rewardVideoListener(RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
    if ([RewardedVideoAdEvent.loaded, RewardedVideoAdEvent.opened, RewardedVideoAdEvent.started, RewardedVideoAdEvent.completed].contains(event)) {
      _logger.d("AdMob | Following RewardedVideoAd Event was fired - $event");
    }

    if (RewardedVideoAdEvent.rewarded == event) {
      _logger.i("AdMob | User granted reward - $rewardType, ($rewardAmount)");
      AdManager.grantUserAdReward(Provider.of<SharedPreferences>(context, listen: false));
    }

    if (RewardedVideoAdEvent.failedToLoad == event) {
      _logger.e("AdMob | failed to load Reward Ad");
    }
  }

The AdManager is (I hope :)) base on your idea,

class AdManager {
  static final _logger = Logger();

  static Ads _ads = Ads(AdManager.appId);

  static void initAds() => _ads ??= Ads(
        AdManager.appId,
        videoUnitId: AdManager.rewardedAdUnitId,
        keywords: <String>['food', 'fit', 'calories'],
        contentUrl: 'https://makromapa.pl',
        childDirected: false,
        testDevices: [],
        testing: false,
        listener: _eventListener,
      );

  static void showRewardAd(RewardedVideoAdListener rewardFunc) async {
    _ads.setVideoAd(
      adUnitId: AdManager.rewardedAdUnitId,
      keywords: <String>['food', 'fit', 'calories'],
      listener: rewardFunc
    );

    _ads.showVideoAd(listener: rewardFunc);
    _ads.dispose();
  }

  static void dispose() => _ads?.dispose();
Marek00Malik commented 3 years ago

only way I can have the listener executed is when I pass the actual function directly when calling setVideoAd

    _ads.setVideoAd(
      adUnitId: AdManager.rewardedAdUnitId,
      keywords: <String>['food', 'fit', 'calories'],
      listener: (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
        if ([RewardedVideoAdEvent.loaded, RewardedVideoAdEvent.opened, RewardedVideoAdEvent.started, RewardedVideoAdEvent.completed].contains(event)) {
          _logger.d("AM AdMob | Following RewardedVideoAd Event was fired - $event");
        }

        if (RewardedVideoAdEvent.rewarded == event) {
          _logger.i("AM AdMob | User granted reward - $rewardType, ($rewardAmount)");
        }

        if (RewardedVideoAdEvent.failedToLoad == event) {
          _logger.e("AM AdMob | failed to load Reward Ad");
        }
      },
    );

I have no clue why is that... ;/

Andrious commented 3 years ago

Let's see if we can't see why the listener doesn't work as expected

How about placing some breakpoints in the class _VidListener found in the file, ads.dart.

Find the function, void eventListener( in that file and place the breakpoints as shown below. Running your app in debug mode will then possibly reveal why things are not running as expected. eventListener