Flutter SDK Usage
Banner&MREC Ads
Once the initialization of the SDK is complete, you can get and load banner and mrec ads from the Empower AppLovin SDK.
Loading a Banner or MREC
Banners
final String _banner_ad_unit_ID = Platform.isAndroid ? "«Android-banner-ad-unit-ID»" : "«iOS-banner-ad-unit-ID»";function initializeBannerAds(){// MAX automatically sizes banners to 320×50 on phones and 728×90 on tabletsAppLovinMAX.createBanner(_banner_ad_unit_ID, AdViewPosition.bottomCenter);}
MRECs
final String _mrec_ad_unit_ID = Platform.isAndroid ? "«Android-MREC-ad-unit-ID»" : "«iOS-MREC-ad-unit-ID»";public void initializeMRecAds(){// MAX sizes MRECs to 300×250 on phones and tabletsAppLovinMAX.createMRec(_mrec_ad_unit_ID, AdViewPosition.bottomCenter);}
Displaying a Banner
To show a banner, call showBanner()
AppLovinMAX.showBanner(_banner_ad_unit_ID);
To hide a banner, call hideBanner()
:
AppLovinMAX.hideBanner(_banner_ad_unit_ID);
Widget Method
You can also render a banner or MREC directly in your widget tree:
Banner
final String _ad_unit_ID = Platform.isAndroid ? "«Android-ad-unit-ID»" : "«iOS-ad-unit-ID»";MaxAdView(adUnitId: _ad_unit_ID,adFormat: AdFormat.banner,listener: AdViewAdListener(onAdLoadedCallback: (ad) {⋮}, onAdLoadFailedCallback: (adUnitId, error) {⋮}, onAdClickedCallback: (ad) {⋮}, onAdExpandedCallback: (ad) {⋮}, onAdCollapsedCallback: (ad) {⋮}))
MRECs
final String _ad_unit_ID = Platform.isAndroid ? "«Android-ad-unit-ID»" : "«iOS-ad-unit-ID»";MaxAdView(adUnitId: _ad_unit_ID,adFormat: AdFormat.mrec,listener: AdViewAdListener(onAdLoadedCallback: (ad) {⋮}, onAdLoadFailedCallback: (adUnitId, error) {⋮}, onAdClickedCallback: (ad) {⋮}, onAdExpandedCallback: (ad) {⋮}, onAdCollapsedCallback: (ad) {⋮}))
App Open Ads
Future<void> main() async {WidgetsFlutterBinding.ensureInitialized();MaxConfiguration? configuration = await AppLovinMAX.initialize(_sdkKey);if (configuration != null) {_isInitialized = true;}runApp(const MyApp());}class MyApp extends StatefulWidget {const MyApp({Key? key}) : super(key: key);@overrideState<MyApp> createState() => _MyAppState();}class _MyAppState extends State<MyApp> with WidgetsBindingObserver {@overridevoid initState() {super.initState();if (_isInitialized) {AppLovinMAX.setAppOpenAdListener(AppOpenAdListener(onAdLoadedCallback: (ad) {},onAdLoadFailedCallback: (adUnitId, error) {},onAdDisplayedCallback: (ad) { },onAdDisplayFailedCallback: (ad, error) {AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId);},onAdClickedCallback: (ad) {},onAdHiddenCallback: (ad) {AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId);},onAdRevenuePaidCallback: (ad) {},));AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId);}WidgetsBinding.instance.addObserver(this);}@overridevoid dispose() {WidgetsBinding.instance.removeObserver(this);super.dispose();}@overrideFuture<void> didChangeAppLifecycleState(AppLifecycleState state) async {switch (state) {case AppLifecycleState.resumed:await showAdIfReady();break;case AppLifecycleState.paused:case AppLifecycleState.inactive:case AppLifecycleState.detached:break;}}Future<void> showAdIfReady() async {if (!_isInitialized) {return;}bool isReady = (await AppLovinMAX.isAppOpenAdReady(_appOpenAdUnitId))!;if (isReady) {AppLovinMAX.showAppOpenAd(_appOpenAdUnitId);} else {AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId);}}⋮}
Interstitial Ads
Load an Interstitial Ad
final String _interstitial_ad_unit_ID = Platform.isAndroid ? "«Android-ad-unit-ID»" : "«iOS-ad-unit-ID»";const int _maxExponentialRetryCount = 6;var _interstitialRetryAttempt = 0;void initializeInterstitialAds() {AppLovinMAX.setInterstitialListener(InterstitialListener(onAdLoadedCallback: (ad) {// Interstitial ad is ready to show. AppLovinMAX.isInterstitialReady(_interstitial_ad_unit_ID) now returns 'true'.print('Interstitial ad loaded from ' + ad.networkName);// Reset retry attempt_interstitialRetryAttempt = 0;},onAdLoadFailedCallback: (adUnitId, error) {// Interstitial ad failed to load.// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)._interstitialRetryAttempt = _interstitialRetryAttempt + 1;if (_interstitialRetryAttempt > _maxExponentialRetryCount) return;int retryDelay = pow(2, min(_maxExponentialRetryCount, _interstitialRetryAttempt)).toInt();print('Interstitial ad failed to load with code ' + error.code.toString() + ' - retrying in ' + retryDelay.toString() + 's');Future.delayed(Duration(milliseconds: retryDelay * 1000), () {AppLovinMAX.loadInterstitial(_interstitial_ad_unit_ID);});},onAdDisplayedCallback: (ad) {⋮},onAdDisplayFailedCallback: (ad, error) {⋮},onAdClickedCallback: (ad) {⋮},onAdHiddenCallback: (ad) {⋮},));// Load the first interstitial.AppLovinMAX.loadInterstitial(_interstitial_ad_unit_ID);}
Showing an Interstitial Ad
bool isReady = (await AppLovinMAX.isInterstitialReady(_interstitial_ad_unit_ID))!;if (isReady) {AppLovinMAX.showInterstitial(_interstitial_ad_unit_ID);}
Rewarded Ads
Load a Rewarded Ad
final String _rewarded_ad_unit_ID = Platform.isAndroid ? "«Android-ad-unit-ID»" : "«iOS-ad-unit-ID»";const int _maxExponentialRetryCount = 6;var _rewardedAdRetryAttempt = 0;void initializeRewardedAd() {AppLovinMAX.setRewardedAdListener(RewardedAdListener(onAdLoadedCallback: (ad) {// Rewarded ad is ready to show. AppLovinMAX.isRewardedAdReady(_rewarded_ad_unit_ID) now returns 'true'.print('Rewarded ad loaded from ' + ad.networkName);// Reset retry attempt_rewardedAdRetryAttempt = 0;}, onAdLoadFailedCallback: (adUnitId, error) {// Rewarded ad failed to load.// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)._rewardedAdRetryAttempt = _rewardedAdRetryAttempt + 1;if (_rewardedAdRetryAttempt > _maxExponentialRetryCount) return;int retryDelay = pow(2, min(_maxExponentialRetryCount, _rewardedAdRetryAttempt)).toInt();print('Rewarded ad failed to load with code ' + error.code.toString() + ' - retrying in ' + retryDelay.toString() + 's');Future.delayed(Duration(milliseconds: retryDelay * 1000), () {AppLovinMAX.loadRewardedAd(_rewarded_ad_unit_ID);});}, onAdDisplayedCallback: (ad) {⋮}, onAdDisplayFailedCallback: (ad, error) {⋮}, onAdClickedCallback: (ad) {⋮}, onAdHiddenCallback: (ad) {⋮}, onAdReceivedRewardCallback: (ad, reward) {⋮}));}void loadRewardedAd() { AppLovinMAX.loadRewardedAd(_rewarded_ad_unit_id);}
Showing a Rewarded Ad
bool isReady = (await AppLovinMAX.isRewardedAdReady(_rewarded_ad_unit_ID))!;if (isReady) {AppLovinMAX.showRewardedAd(_rewarded_ad_unit_ID);}