Display rewarded ad on Flutter

Step 1: (Optional) Construct event listener

final AdropRewardedListener listener = AdropRewardedListener(
    onAdReceived: (ad) =>
        debugPrint('Adrop Interstitial Ad loaded with unitId ${ad.unitId}!'),
    onAdFailedToReceive: (ad, errorCode) => 
        debugPrint('error in ${ad.unitId} while loading: $errorCode'),
    onAdFailedToShowFullScreen: (ad, errorCode) =>
        debugPrint('error in ${ad.unitId} while showing: $errorCode'),
    onAdEarnRewardHandler: (ad, type, amount) {
        debugPrint('Adrop Rewarded Ad earn rewards: ${ad.unitId}, $type, $amount');
        // implement your actions with rewards
    },
    ...
);

Step 2: Display a rewarded ad

class YourComponent extends StatefulWidget {
  const YourComponent({super.key});

  @override
  State<StatefulWidget> createState() => _YourComponentState();
}

class _YourComponentState extends State<YourComponent> {
  final AdropRewardedAd rewardedAd = AdropRewardedAd(
    unitId: 'YOUR_UNIT_ID',
    listener: listener,
  );

  @override
  void initState() {
    super.initState();
    rewardedAd.load();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () {
            final isLoaded = rewardedAd?.isLoaded ?? false;
            if (isLoaded) {
              rewardedAd.show();
            } else {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('rewarded ad is loading...'))
              );
            }
          },
          child: const Text('display ad'),
        ),
      ),
    );
  }
}

AdropRewardedAd must be disposed of when access to it is no longer needed.

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

Last updated