bdlukaa / native_admob_flutter

Easy-to-make native ads in flutter using AdMOB SDK.
https://pub.dev/packages/native_admob_flutter
BSD 3-Clause "New" or "Revised" License
81 stars 57 forks source link

Cancel/reset rewardAd events listener #123

Closed erperejildo closed 2 years ago

erperejildo commented 2 years ago

I have different pages where I need to display a reward ad. The events work fine on all of them but since I open a new ad, a new listener is created.

Provider.of<AdState>(context, listen: false).showRewardedAd();
              Provider.of<AdState>(context, listen: false)
                  .rewardedAd
                  .onEvent
                  .listen((Map<RewardedAdEvent, dynamic> e) {
                final event = e.keys.first;
                switch (event) {
                  case RewardedAdEvent.earnedReward:
                    print('*** Rewarded Ad successful.');
                    break;
                }
              });
...
Future<void> showRewardedAd() async {
    rewardedAd.onEvent.listen((event) {}).cancel(); // doesn't work
    if (!rewardedAd.isAvailable) {
      await rewardedAd.load();
    }
    await rewardedAd.show();
    rewardedAd.load();
}

How could I remove all the previous ones?

erperejildo commented 2 years ago

Fixed like this:

if (!rewardedAd.isAvailable) {
      await rewardedAd.load();
    }
    await rewardedAd.show();
    // cancel previous listeners
    rewardedAd = await RewardedAd()
      ..load();