我试图在听众打印广告加载之前展示广告
Im trying to show ad before listener prints ad loaded
@override
void didChangeDependencies() {
super.didChangeDependencies();
loadInter();
}
loadInter() {
final adState = Provider.of<AdState>(context);
adState.initialization.then((status) {
interAd = InterstitialAd(
adUnitId: adState.bannerAdUnitId,
request: AdRequest(),
listener: adState.listener);
interAd.load();
});
}
我有点不确定放在哪里 interAd.show();
我想在听众打印时调用它 I/flutter (10349): Ad loaded.
但我一直提前调用它并得到 E/FlutterInterstitialAd(10349): The interstitial wasn't loaded yet.
adstate class
class AdState {
Future<InitializationStatus> initialization;
AdState(this.initialization);
String get bannerAdUnitId => Platform.isAndroid
? "ca-app-pub-3940256099942544/1033173712"
: "ca-app-pub-3940256099942544/4411468910";
final AdListener listener = AdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) {
print('Ad loaded.');
},
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('Ad 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 ad is in the process of leaving the application.
onApplicationExit: (Ad ad) => print('Left application.'),
);
}
我想在调用侦听器 onAdLoaded(Ad ad)
时执行 show();
。我尝试为状态 class 实现一个 AdListener,但在我的状态 class.
中没有调用该函数
您似乎没有await
希望初始化正确完成。尝试将 loadInter
方法标记为 asnyc
和 await
以完成初始化。然后你可以加载广告,然后你可以添加一个 setter 到你的 AdState
class,传递加载的广告以在听众通知你广告已加载时显示它。
class AdState {
Future<InitializationStatus> initialization;
// This field will let you store the ad.
InterstitialAd _ad;
AdState(this.initialization);
void setAd(InterstitialAd ad) {
_ad = ad;
}
String get bannerAdUnitId => Platform.isAndroid
? "ca-app-pub-3940256099942544/1033173712"
: "ca-app-pub-3940256099942544/4411468910";
final AdListener listener = AdListener(
// Called when an ad is successfully received.
onAdLoaded: (_) {
if (_ad != null) {
_ad.show();
}
},
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('Ad 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 ad is in the process of leaving the application.
onApplicationExit: (Ad ad) => print('Left application.'),
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
loadInter();
}
void loadInter() async {
final adState = Provider.of<AdState>(context);
adState.setAd(interAd);
final status = await adState.initialization;
interAd = InterstitialAd(
adUnitId: adState.bannerAdUnitId,
request: AdRequest(),
listener: adState.listener);
await interAd.load();
}
更新广告并在 InterstitialAd
的侦听器为您提供 .loaded
事件时显示广告可能更清晰。有关详细信息和代码示例,请参阅 this medium article on how to add AdMob in Flutter。
@override
void didChangeDependencies() {
super.didChangeDependencies();
loadInter();
}
loadInter() {
final adState = Provider.of<AdState>(context);
adState.initialization.then((status) {
interAd = InterstitialAd(
adUnitId: adState.bannerAdUnitId,
request: AdRequest(),
listener: adState.listener);
interAd.load();
});
}
我有点不确定放在哪里 interAd.show();
我想在听众打印时调用它 I/flutter (10349): Ad loaded.
但我一直提前调用它并得到 E/FlutterInterstitialAd(10349): The interstitial wasn't loaded yet.
adstate class
class AdState {
Future<InitializationStatus> initialization;
AdState(this.initialization);
String get bannerAdUnitId => Platform.isAndroid
? "ca-app-pub-3940256099942544/1033173712"
: "ca-app-pub-3940256099942544/4411468910";
final AdListener listener = AdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) {
print('Ad loaded.');
},
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('Ad 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 ad is in the process of leaving the application.
onApplicationExit: (Ad ad) => print('Left application.'),
);
}
我想在调用侦听器 onAdLoaded(Ad ad)
时执行 show();
。我尝试为状态 class 实现一个 AdListener,但在我的状态 class.
您似乎没有await
希望初始化正确完成。尝试将 loadInter
方法标记为 asnyc
和 await
以完成初始化。然后你可以加载广告,然后你可以添加一个 setter 到你的 AdState
class,传递加载的广告以在听众通知你广告已加载时显示它。
class AdState {
Future<InitializationStatus> initialization;
// This field will let you store the ad.
InterstitialAd _ad;
AdState(this.initialization);
void setAd(InterstitialAd ad) {
_ad = ad;
}
String get bannerAdUnitId => Platform.isAndroid
? "ca-app-pub-3940256099942544/1033173712"
: "ca-app-pub-3940256099942544/4411468910";
final AdListener listener = AdListener(
// Called when an ad is successfully received.
onAdLoaded: (_) {
if (_ad != null) {
_ad.show();
}
},
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('Ad 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 ad is in the process of leaving the application.
onApplicationExit: (Ad ad) => print('Left application.'),
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
loadInter();
}
void loadInter() async {
final adState = Provider.of<AdState>(context);
adState.setAd(interAd);
final status = await adState.initialization;
interAd = InterstitialAd(
adUnitId: adState.bannerAdUnitId,
request: AdRequest(),
listener: adState.listener);
await interAd.load();
}
更新广告并在 InterstitialAd
的侦听器为您提供 .loaded
事件时显示广告可能更清晰。有关详细信息和代码示例,请参阅 this medium article on how to add AdMob in Flutter。