flutter - Flutter google_mobile_ads 在广告上通知 UI 无法加载
问题描述
我是 Flutter google_mobile_ads 的新手。我已经使用颤振团队的 YouTube 教程 ( https://www.youtube.com/watch?v=m0d_pbgeeG8&t=1189s&ab_channel=Flutter ) 实施了 Google Ads。
在本教程中,我们将 getAdListener 方法放在另一个类(Provider)中。我的问题是,如果广告无法加载,如何从 UI 中删除该特定广告而不是显示空白?
以下是我的视图类。
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadAds();
}
_loadAds() {
final _adState = Provider.of<AdState>(context);
_banner1 = await _adState.getBannerAd();
_banner2 = await _adState.getBannerAd();
}
_showAds() {
return Column(
children: [
if (_banner1 != null)
/*
problem: _banner1 never comes null even if the ad failed to load. if it fails how can I check and not show the _banner1 empty space?
*/
Container(child: AdWidget(ad: _banner1)),
if (_banner2 != null)
Container(child: AdWidget(ad: _banner2)),
]
);
}
我的 AdState (provider) 有 getBannerAd() 方法。
// This method will load the banner ad
Future<BannerAd> getBannerAd() async {
await this.initialization;
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
size: AdSize.banner,
request: AdRequest(),
listener: this.bannerAdListener)
..load();
}
BannerAdListener _bannerAdListener = BannerAdListener(
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
// Now I will have to tell my UI that this particular ad failed to load and not show it
},);
谢谢
解决方案
推荐阅读
- php - SQL将值/参数绑定到准备好的语句中不起作用
- php - 在多维数组中查找重复值
- amazon-web-services - 是否可以指示 AWS 自定义授权者根据阶段变量调用 AWS Lambda?
- xml - XSI 命名空间复制到结果序列
- javascript - 弹出窗口应每次访问显示一次,而是每次显示
- gruntjs - grunt-modernizr 没有输出
- mule - 在 mule 中从 FTP 读取文件
- java - 如果应用程序当前无法处理从 MQ 异步获取的 get 消息,它会保留在哪里?
- linux - 某些目录中的文件可能与隐式目录中的库冲突
- python - Tkinter 画布未显示