如何在flutter项目的一个屏幕中使用多个Admob横幅



我有flutter项目,我在屏幕中添加了一个横幅广告,但我需要在同一屏幕中加载更多广告,我试着在不同的地方重复调用函数

======== Exception caught by widgets library =======================================================
The following assertion was thrown building AdWidget(dirty, state: _AdWidgetState#72c34):
This AdWidget is already in the Widget tree

If you placed this AdWidget in a list, make sure you create a new instance in the builder function with a unique ad object.
Make sure you are not using the same ad object in more than one AdWidget.

我可以在同一屏幕上加载多个横幅吗?

这是Github上的链接https://github.com/Hussamedeen/MyDealApp

主页屏幕.dart

由于您没有提供任何代码,我会根据我认为您可以做的事情来假设和回答。

如果您在预定义的固定位置显示广告,则需要为每个位置创建一个新的BannerAd。您还必须单独加载这些BannerAd,如下所示:

final BannerAd myBanner1 = BannerAd(
adUnitId: '<Banner ID>',
size: AdSize.smartBanner,
request: AdRequest(),
listener: AdListener(onAdClosed: (ad) => ad.dispose()),
);
myBanner1.load();
final adWidget1 = AdWidget(ad: myBanner1);
...
...
...
final BannerAd myBannerNth = BannerAd(
adUnitId: '<Banner ID>',
size: AdSize.banner,
request: AdRequest(),
listener: AdListener(onAdClosed: (ad) => ad.dispose()),
);
myBannerNth.load();
final adWidgetNth = AdWidget(ad: myBannerNth);

其中myBannerNth/adWidgetNth是第n个横幅/ad小部件。

对于动态的、自动生成的情况,例如在ListView.separated中,您可以这样做:

// Defined somewhere, e.g. in your State[less/ful] widget
Map<String, BannerAd> ads = <String, BannerAd>{}; 
...
...
...
ListView.separated(
separatorBuilder: (context, index) => Divider(),
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
ads['myBanner$index'] = BannerAd(
adUnitId: '<Banner ID>',
size: AdSize.banner,
request: AdRequest(),
listener: AdListener(onAdClosed: (ad) => ad.dispose()));
ads['myBanner$index'].load();
if (index % 6 == 0) {
return Column(
children: [
Container(
child: AdWidget(ad: ads['myBanner$index']),
height: 100.0,
),
_buildItem(context, items[index])
],
);
}
return _buildItem(context, items[index]);
},
itemCount: items.length,
)

其中adsMap,其值是单独动态自动生成的横幅广告。

您可以使用此功能显示多个广告:

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
class AppAds {
static bannerAds(BuildContext context) {
return Builder(builder: (ctx) {
final BannerAd myBanner = BannerAd(
adUnitId: 'ca-app-pub-3940256099942544/6300978111',
request: const AdRequest(),
listener: const BannerAdListener(),
size: AdSize.banner,
);
myBanner.load();
return Container(
alignment: Alignment.center,
width: myBanner.size.width.toDouble(),
height: myBanner.size.height.toDouble(),
child: AdWidget(
ad: myBanner,
key: Key(myBanner.hashCode.toString()),
),
);
});
}
}

来源:

https://github.com/googleads/googleads-mobile-flutter/issues/36

还有其他方法可以做到这一点,例如在包的示例文件夹中创建另一个有状态类。

静态Future getBannerWidget({@必需的BuildContext上下文,AdSize AdSize,})异步{

BannerAd bannerAd = BannerAd(
adUnitId: getBannerAdUnitId(),
size: adSize ?? AdSize.smartBanner,
request: _adRequest,
listener: AdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) {
if(_debug){
print('Ad loaded.');
}
},
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
if(_debug){
print('Ad failed to load: $error');
//_bannerAd.dispose();
}
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) {
if(_debug){
print('Ad opened.');
}
},
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) {
if(_debug){
print('Ad closed.');
}
},
// Called when an ad is in the process of leaving the application.
onApplicationExit: (Ad ad) {
if(_debug){
print('Left application.');
}
},
),
);
await bannerAd.load();
return Container(
child: AdWidget(ad: bannerAd),
constraints: BoxConstraints(
maxHeight: 90,
maxWidth: MediaQuery.of(context).size.width,
minHeight: 32,
minWidth: MediaQuery.of(context).size.width,
),
);
}

屏幕中:

FutureBuilder<Widget>(
future: Ads.getBannerWidget(
context: context,
adSize: AdSize.leaderboard
),
builder: (_, snapshot){
if(!snapshot.hasData){
return Text("Loading Ad...");
} else {
return Container(
height: 90,
width: MediaQuery.of(context).size.width,
child: snapshot.data,
);
}
},
),

最新更新