Sachin Saini
 · FlutterFlow Expert

AdMob Rewarded Ad [Cracked]

Integrate Google Admob Rewarded Ads in FlutterFlow with Easy Steps

Step 1: Create a Custom Action: googleAdMobRewardedAd

Step 2: Add Dependencies (optional): google_mobile_ads: ^4.0.0

Step 3: Define the Arguments (nullable String): adUnitID

Step 4: Copy And Paste the below code to Code Editor:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'dart:async';
import 'package:google_mobile_ads/google_mobile_ads.dart';

Future<bool> googleAdMobRewardedAd(String? adUnitID) async {
  Completer<bool> adLoadedCompleter = Completer<bool>();

  RewardedAd? _rewardedAd;

  void _createRewardedAd() {
    RewardedAd.load(
      adUnitId: adUnitID!,
      request: AdRequest(),
      rewardedAdLoadCallback: RewardedAdLoadCallback(
        onAdLoaded: (RewardedAd ad) {
          print('$ad loaded.');
          _rewardedAd = ad;
          adLoadedCompleter.complete(true);
        },
        onAdFailedToLoad: (LoadAdError error) {
          print('RewardedAd failed to load: $error');
          _rewardedAd = null;
          adLoadedCompleter.completeError(error.message);
        },
      ),
    );
  }

  void _showRewardedAd() {
    if (_rewardedAd == null) {
      print('Warning: attempt to show rewarded before loaded.');
      adLoadedCompleter
          .completeError('RewardedAd is null. Call _createRewardedAd first.');
      return;
    }

    _rewardedAd!.fullScreenContentCallback = FullScreenContentCallback(
      onAdShowedFullScreenContent: (RewardedAd ad) =>
          print('ad onAdShowedFullScreenContent.'),
      onAdDismissedFullScreenContent: (RewardedAd ad) {
        print('$ad onAdDismissedFullScreenContent.');
        ad.dispose();
        _createRewardedAd();
      },
      onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
        print('$ad onAdFailedToShowFullScreenContent: $error');
        ad.dispose();
        _createRewardedAd();
      },
    );

    _rewardedAd!.setImmersiveMode(true);
    _rewardedAd!.show(
      onUserEarnedReward: (Ad ad, RewardItem reward) {
        print('$ad with reward $RewardItem(${reward.amount}, ${reward.type})');
      },
    );
    _rewardedAd = null;
  }

  _createRewardedAd(); // Call this function to load the rewarded ad when the custom action is invoked.

  try {
    bool adLoaded = await adLoadedCompleter.future;
    if (adLoaded) {
      _showRewardedAd(); // Call this function to show the rewarded ad.
      return true;
    } else {
      return false;
    }
  } catch (error) {
    print('Error loading rewarded ad: $error');
    return false;
  }
}

Step 5: Define the Ad Unit ID in the Action Block

Don't Forget to Add an action after the AdShown.

That's All!

If you need any help in Integration you can Get help via Book 1:1 Session

8
17 replies