How to open another page after rewarded ad is watched in Flutter?

601

Use the following code on your first page above the build method

late RewardedAd _rewardedAd;
  bool _isRewardedAdReady = false;

  // TODO: Implement _loadRewardedAd()
  void _loadRewardedAd() {
    RewardedAd.load(
      adUnitId: AdHelper.rewardedAdUnitId,
      request: AdRequest(),
      rewardedAdLoadCallback: RewardedAdLoadCallback(
        onAdLoaded: (ad) {
          this._rewardedAd = ad;

          ad.fullScreenContentCallback = FullScreenContentCallback(
            onAdDismissedFullScreenContent: (ad) {
              setState(() {
                _isRewardedAdReady = false;
              });
              _loadRewardedAd();
            },
          );

          setState(() {
            _isRewardedAdReady = true;
          });
        },
        onAdFailedToLoad: (err) {
          print('Failed to load a rewarded ad: ${err.message}');
          setState(() {
            _isRewardedAdReady = false;
          });
        },
      ),
    );
  }

After that initialize and dispose the rewardedad like

@override
  void initState() {
    _loadRewardedAd();
    super.initState();
  }

  @override
  void dispose() {
    _rewardedAd.dispose();
    super.dispose();
  }

Now call the rewarede ad with Navigating to othe page

GestureDetector(
       onTap: () {
        if(_isRewardedAdReady){
        _rewardedAd.show(onUserEarnedReward:
                           (RewardedAd ad, RewardItem reward) {
                                print(
                                    '$ad with reward $RewardItem(${reward.amount}, ${reward.type}');
       });
     }
     Navigator.pushReplacement(
     context, MaterialPageRoute(builder: (context)=>SecondPage()));
        },
          child: CustomButton(text:'GoTo Second Page')
         ),

for better understand go to here

Share:
601
stanojkowski
Author by

stanojkowski

Updated on November 30, 2022

Comments

  • stanojkowski
    stanojkowski over 1 year

    Flutter App. One button that leads to the 2nd page where the content is.

    • The user clicks on the button and must watch a video ad(rewarded ad).

    After the video ad finishes > the user can open the 2nd page / or the 2nd page will be automatically opened when he clicks 'x' on the finished video ad.

    My question is > how to do this? What would the code look like and what to use? Thank you!

    • Admin
      Admin over 2 years
      Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.