gpt4 book ai didi

dart - Flutter firebase_admob 仅显示一次

转载 作者:行者123 更新时间:2023-12-01 17:58:35 25 4
gpt4 key购买 nike

以下代码在应用程序底部显示横幅广告,并在点击时显示插页式广告。按下按钮时会显示插页式广告。问题是,当应用程序第一次启动并单击按钮时,会显示插页式广告,但从第二次开始,不再显示插页式广告。没有错误,所有日志消息都显示成功。我的代码有错误吗?我正在 Android 设备上进行测试。

import 'package:firebase_admob/firebase_admob.dart';
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {

final admobAppId = 'ca-app-pub-3940256099942544~3347511713';
final bannerId = 'ca-app-pub-3940256099942544/6300978111';
final interstitialId = 'ca-app-pub-3940256099942544/1033173712';

BannerAd bannerAd;
InterstitialAd interstitialAd;

MyApp() {
FirebaseAdMob.instance.initialize(appId: admobAppId);
makeBannerAd();
initInterstitialAd();
}

makeBannerAd() {
bannerAd = BannerAd(
adUnitId: bannerId,
size: AdSize.smartBanner,
listener: (MobileAdEvent me) {
print('MobileAdEvent $me');
});
bannerAd
..load()
..show();
}

initInterstitialAd() {
interstitialAd = InterstitialAd(
adUnitId: interstitialId,
listener: (MobileAdEvent me) {
print(
'========== Interstitial ad mobile ad event =========== \n $me');
if (me == MobileAdEvent.closed) {
print('Interstitial closed');
loadInterstitialAd();
}
});
loadInterstitialAd();
}

loadInterstitialAd() {
interstitialAd.load().then((val) {
if (val) {
print('Interstitial ad loaded callback success');
} else {
print('Interstitial ad loaded callback failed');
}
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AdMob plugin test'),
),
body: Center(
child: RaisedButton(
child: Text('Show Interstitial!'),
onPressed: () {
interstitialAd.show().then((val) {
if (val) {
print('Interstitial ad show callback success');
} else {
print('Interstitial ad show callback fail');
}
});
},
),
),
),
);
}
}

更新
以下是 dispose() 的代码。最多显示 2 次。

class MyApp extends StatelessWidget {

final admobAppId = 'ca-app-pub-3940256099942544~3347511713';
final bannerId = 'ca-app-pub-3940256099942544/6300978111';
final interstitialId = 'ca-app-pub-3940256099942544/1033173712';

BannerAd bannerAd;
InterstitialAd interstitialAd;
MobileAdTargetingInfo mobileAdTargetingInfo;

MyApp() {
FirebaseAdMob.instance.initialize(appId: admobAppId);
makeBannerAd();
initInterstitialAd();
}

makeBannerAd() {
bannerAd = BannerAd(
adUnitId: bannerId,
size: AdSize.smartBanner,
listener: (MobileAdEvent me) {
print('Banner => MobileAdEvent $me');
});
bannerAd
..load()
..show();
}

initInterstitialAd() {
interstitialAd = InterstitialAd(
adUnitId: interstitialId,
listener: (MobileAdEvent me) {
print(
'========== Interstitial ad mobile ad event =========== $me');
if (me == MobileAdEvent.closed) {
print('Interstitial closed');
interstitialAd.dispose().then((val){
if(val){
loadInterstitialAd();
}else{
}
});
} else if (me == MobileAdEvent.failedToLoad) {
print('Interstitial failed to load');
loadInterstitialAd();
}
});
loadInterstitialAd();
}

loadInterstitialAd() {
interstitialAd.load();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AdMob plugin test'),
),
body: Center(
child: RaisedButton(
child: Text('Show Interstitial!'),
onPressed: () {
interstitialAd.show();
},
),
),
),
);
}
}

最佳答案

我遇到了同样的问题,并使用 InterstitialAd 对象解决了该问题,如下所示:

InterstitialAd myInterstitial() {
return InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
if (event == MobileAdEvent.failedToLoad) {
interstitialAd..load();
} else if (event == MobileAdEvent.closed) {
interstitialAd = myInterstitial()..load();
}
},
);
}
@override
void initState() {
FirebaseAdMob.instance.initialize(appId:
FirebaseAdMob.testAppId);
interstitialAd = myInterstitial()..load();
super.initState();
}
@override
void dispose() {
interstitialAd?.dispose();
super.dispose();
}

调用方法:

interstitialAd
..load()
..show();

根据我的理解,您可以在事件关闭时使用监听器递归调用初始 InterstitialAd 对象。我的来源来自Tensor Programming Youtube Channel他解释得比我好得多。

关于dart - Flutter firebase_admob 仅显示一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54516583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com