Flutter, Google’s open-source UI software development kit, is a popular choice for building natively compiled applications for mobile, web, and desktop from a single codebase. By integrating Google AdMob, you can monetize these applications effectively. Here’s a guide to get you started.
Before you can start developing and monetizing apps, ensure you have the necessary tools installed.
flutter create my_monetized_app
cd my_monetized_app
Sign Up for AdMob: Create an AdMob account at admob.google.com.
Create an Ad Unit:
pubspec.yaml
file and add the google_mobile_ads
dependency:
dependencies:
flutter:
sdk: flutter
google_mobile_ads: ^1.0.0
main.dart
file, initialize AdMob:
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AdMob in Flutter'),
),
body: MyHomePage(),
),
);
}
}
Load and Display Ads:
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
BannerAd? _bannerAd;
@override
void initState() {
super.initState();
_bannerAd = BannerAd(
adUnitId: '<YOUR-AD-UNIT-ID>',
size: AdSize.banner,
request: AdRequest(),
listener: BannerAdListener(),
)..load();
}
@override
void dispose() {
_bannerAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (_bannerAd != null)
Container(
alignment: Alignment.center,
child: AdWidget(ad: _bannerAd!),
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
),
],
);
}
}
Interstitial Ads:
InterstitialAd? _interstitialAd;
void _createInterstitialAd() {
InterstitialAd.load(
adUnitId: '<YOUR-AD-UNIT-ID>',
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) {
_interstitialAd = ad;
},
onAdFailedToLoad: (err) {
print('Failed to load an interstitial ad: ${err.message}');
},
));
}
void _showInterstitialAd() {
if (_interstitialAd != null) {
_interstitialAd!.show();
}
}
@override
void dispose() {
_interstitialAd?.dispose();
super.dispose();
}
Before releasing your app, test your ads using AdMob’s test ad units to ensure they display correctly and don’t affect the user experience negatively.
Once you’ve thoroughly tested your app and integrated ads, you can publish it to the Google Play Store and/or Apple App Store. Follow the respective guidelines for publishing on Google Play and publishing on the App Store.
Monetizing your Flutter app using Google AdMob can be a lucrative way to generate income from your hard work. By following the steps outlined above, you can integrate ads seamlessly into your app, ensuring a steady revenue stream while providing value to your users. Always adhere to best practices and Google’s policies to maintain a positive user experience and maximize your earnings.
Источник: dev.to
Наш сайт является информационным посредником. Сообщить о нарушении авторских прав.
flutter dart beginners programming