我已经迁移到 Flutter 中新的 Google 移动广告包。我的插页式广告效果很好,但我的原生广告在加载到我的 ListView 容器之前会在我的大部分屏幕上显示一个临时广告,等待它使用 FutureBuilder。如果我继续滚动,它最终会按需要显示。我不确定这是否只是模拟器的问题和我的代码的更大问题。这是问题的一个示例...另外,我不确定它是否是我的 xib 文件,但我使用的是 googleads-moble-flutter/NativeAds 示例中的 1 HERE
这是我的代码示例....
Future futureAd;
@override
void initState() {
super.initState();
futureAd = adBuilder();
}
Future adBuilder() async {
// TODO: Create a NativeAd instance
final myAd = NativeAd(
adUnitId: NativeAd.testAdId,
factoryId: 'listTile',
request: AdRequest(keywords: ['kpop', 'music', 'youth']),
listener: AdListener(
onAdLoaded: (_) {
setState(() {
_isAdLoaded = true;
});
},
onAdFailedToLoad: (ad, error) {
// Releases an ad resource when it fails to load
ad.dispose();
setState(() {
_isAdLoaded = false;
});
print(
'NATIVE AD ERROR ---- Ad load failed (code=${error.code} message=${error.message})');
},
),
);
_ad = myAd;
_ad.load();
print(_isAdLoaded);
}
FutureBuilder(
future: futureAd,
// ignore: missing_return
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case (ConnectionState.none):
return Container();
case (ConnectionState.done):
return Container(
height: 300,
width:
MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.amberAccent,
borderRadius:
BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 15,
offset: Offset(
4, 8), // Shadow position
),
],
),
child: (_isAdLoaded = true) ? AdWidget(ad: _ad) :
Image.asset('assets/loading.gif'),
);
case (ConnectionState.active):
return Container(
height: 300,
width: MediaQuery.of(context).size.width,
child: Image.asset('assets/loading.gif')
);
case (ConnectionState.waiting):
return Container(
height: 300,
width: MediaQuery.of(context).size.width,
child: Image.asset('assets/loading.gif')
);
default:
return Container(
height: 300,
width: MediaQuery.of(context).size.width,
child: Image.asset('logo500.jpeg')
);
}
},
),