I want to open a RewardedaD when the user clicks on a button (Hint button), but the Rewarded Ad does not show on the first click, its loads and then stops, but the RewardedAd shows when i click the button a second time, i followed the tutorial from https://codelabs.developers.google.com/codelabs/admob-ads-in-flutter#3
I don't know what i have done wrong.
here is my code:
void _loadRewardedAd(BuildContext context) {
//get a reward ad ready
if (MobileAds.instance == null) {
MobileAds.instance.initialize();
}
RewardedAd.load(
adUnitId: AdHelper.rewardedAdUnitId,
request: AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (ad) {
_rewardedAd = ad;
_isRewardedAdReady = true;
_isHearIconPressed = false;
_isGetLivesPressed = false;
_hintPressed = false;
// isWumupsCryButtonPressed = false;
// isWumupsCryButtonActive = false;
setState(() {
//done fetching the ad
});
createLives(currentLivesLeft);
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Ad is ready")));
},
onAdFailedToLoad: (err) {
// _rewardedAd.dispose();
print("Unable to load aD ---------------- ${err.message}");
_isRewardedAdReady = false;
_isGetLivesPressed = false;
_hintPressed = false;
_isHearIconPressed = false;
// isWumupsCryButtonActive = false;
// isWumupsCryButtonPressed = false;
createLives(currentLivesLeft);
setState(() {});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("No Internet, Please Watch an Ad to get Life")));
},
),
);
}
the code above is supposed to get the rewardAd ready, and below is my code for calling the RewardAd to be displayed.
onTap: (this._hintPressed)
? null
: () {
_loadRewardedAd(context); //get the rewarded ad ready
_hintPressed = true; //So i can show a CircleProgressIndicator() on the bulb Button
setState(() {}); //rebuild to the CircleProgressIndicator() so it can show
if (_isRewardedAdReady) {
_rewardedAd!.show( // show the reward ad
onUserEarnedReward:
(RewardedAd ad,RewardItem item) {
createHint(widget.questions[currentQuestionIndex].answer); //show the hint
showHint = true; // so the hint can always be shown
_hintPressed = false; // remove the CircleProgressIndicator();
setState(() {}); //rebuild so everything can be updated.
},
);
_rewardedAd!.fullScreenContentCallback = FullScreenContentCallback(onAdShowedFullScreenContent: (ad) {},
onAdDismissedFullScreenContent: (ad) { // when the ad is dismissed by the user
_isRewardedAdReady = false; // set the rewardedAd to false so we can call it again
_hintPressed = false; // remove the circleprogressIndicator
_isHearIconPressed = false; // remove the circleProgressIndicator from the heart
setState(() {});
_rewardedAd.dispose(); //dispose the rewardedAd
},
onAdFailedToShowFullScreenContent: (ad, error) {
_isRewardedAdReady = false;
_hintPressed = false;
_isHearIconPressed = false;
_isGetLivesPressed = false;
setState(() {});
});
}
},