找不到带有以下内容的广告。原生广告 Flutter google_mobile_ads
Ad with the following id could not be found. Natiev Ad Flutter google_mobile_ads
原生广告显示错误。我使用来自 google.
的测试广告 ID
“此广告可能尚未加载或已被处理。具有以下 ID 的广告不能 found:1”
谁能帮帮我?
编辑 #1 我的代码
原生广告有不同的示例代码吗?
我使用 google_mobile_ads: ^0.13.2
...
class _GameState extends State<Game> {
static const AdRequest targetingInfo = AdRequest(
);
static const int maxFailedLoadAttempts = 3;
static const _adUnitIDNative = "ca-app-pub-3940256099942544/2247696110";
late StreamSubscription _subscription;
bool? _deutsch;
final NativeAd myNative = NativeAd(
adUnitId: _adUnitIDNative,
factoryId: 'listTile',
request: AdRequest(),
listener: NativeAdListener(),
);
final NativeAdListener listener = NativeAdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) => print('Ad loaded.'),
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
// Dispose the ad here to free resources.
ad.dispose();
print('NativeAd failed to load: $error');
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) => print('Ad opened.'),
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) => print('Ad closed.'),
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) => print('Ad impression.'),
// Called when a click is recorded for a NativeAd.
onNativeAdClicked: (NativeAd ad) => print('Ad clicked.'),
);
get adContainer => null;
@override
void initState() {
super.initState();
_createInterstitialAd();
myNative.load();
}
...
Container(
alignment: Alignment.center,
child: AdWidget(ad: myNative),
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(bottom: 20.0),
height: 70,
),
谢谢
您的 AdWidget 在广告加载完成之前正在构建;你需要实现三件事:
- 布尔标志:表示广告已完成加载;
- 侦听事件:加载广告时,将标志设置为 true 并触发重建;和
- 触发小部件:如果为真,则构建广告小部件。
// 1. Create bool
bool isAdLoaded = false;
// 2. Add listener event
final NativeAdListener listener = NativeAdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) => {
setState(() {
isAdLoaded = true;
});
...
// 3. Wrap the AdWidget inside a switch
isAdLoaded ? Container(
alignment: Alignment.center,
child: AdWidget(ad: myNative),
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(bottom: 20.0),
height: 70,
) : CircularProgressIndicator(),
原生广告显示错误。我使用来自 google.
的测试广告 ID
“此广告可能尚未加载或已被处理。具有以下 ID 的广告不能 found:1”
谁能帮帮我?
编辑 #1 我的代码 原生广告有不同的示例代码吗? 我使用 google_mobile_ads: ^0.13.2
...
class _GameState extends State<Game> {
static const AdRequest targetingInfo = AdRequest(
);
static const int maxFailedLoadAttempts = 3;
static const _adUnitIDNative = "ca-app-pub-3940256099942544/2247696110";
late StreamSubscription _subscription;
bool? _deutsch;
final NativeAd myNative = NativeAd(
adUnitId: _adUnitIDNative,
factoryId: 'listTile',
request: AdRequest(),
listener: NativeAdListener(),
);
final NativeAdListener listener = NativeAdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) => print('Ad loaded.'),
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
// Dispose the ad here to free resources.
ad.dispose();
print('NativeAd failed to load: $error');
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) => print('Ad opened.'),
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) => print('Ad closed.'),
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) => print('Ad impression.'),
// Called when a click is recorded for a NativeAd.
onNativeAdClicked: (NativeAd ad) => print('Ad clicked.'),
);
get adContainer => null;
@override
void initState() {
super.initState();
_createInterstitialAd();
myNative.load();
}
...
Container(
alignment: Alignment.center,
child: AdWidget(ad: myNative),
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(bottom: 20.0),
height: 70,
),
谢谢
您的 AdWidget 在广告加载完成之前正在构建;你需要实现三件事:
- 布尔标志:表示广告已完成加载;
- 侦听事件:加载广告时,将标志设置为 true 并触发重建;和
- 触发小部件:如果为真,则构建广告小部件。
// 1. Create bool
bool isAdLoaded = false;
// 2. Add listener event
final NativeAdListener listener = NativeAdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) => {
setState(() {
isAdLoaded = true;
});
...
// 3. Wrap the AdWidget inside a switch
isAdLoaded ? Container(
alignment: Alignment.center,
child: AdWidget(ad: myNative),
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(bottom: 20.0),
height: 70,
) : CircularProgressIndicator(),