dependencies:
google_mobile_ads: ^0.11.0+4
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-aaaaaaaaaaaaaaaa~bbbbbbbbbb"/>
//자신의 AdMob 앱 ID
</application>
</manifest>
dependencies {
classpath 'com.android.tools.build:gradle:3.5.4'
}
android {
compileSdkVersion 29 //28이상(필수)
defaultConfig {
minSdkVersion 21//21이상 권장
targetSdkVersion 29 // 19이상(필수)
multiDexEnabled true//minSdkVersion 21이상일 경우 설정
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MyApp());
}
커맨드 창에서
$ flutter pub get
명령어를 입력한다.
import 'package:google_mobile_ads/google_mobile_ads.dart';
final BannerAd myBanner = BannerAd(
adUnitId: 'ca-app-pub-aaaaaaaaaaaaaaaa/bbbbbbbbbb', //자신의 UnitID
size: AdSize.banner,
request: AdRequest(),
listener: AdListener(),
);
추가로 아래 변수들을 선언해준다.
AdWidget adWidget;
Container adContainer;
@override
void initState() {
Future(() async => await myBanner.load()).then((_) {
if (!mounted) return;
setState(() {
adWidget = AdWidget(
ad: myBanner,
);
adContainer = Container(
alignment: Alignment.center,
child: adWidget,
width: myBanner.size.width.toDouble(),
height: myBanner.size.height.toDouble(),
);
});
});
super.initState();
}
adContainer ?? Container(),
위의 내용은 '제임쓰 Dev'님의 유튜브를 참고하였습니다.
해당 링크: https://www.youtube.com/watch?v=DHmmce1nFlo