【问题标题】:Why is my interstitial ad not showing in Flutter?为什么我的插页式广告没有在 Flutter 中展示?
【发布时间】:2021-12-06 12:19:57
【问题描述】:

我正在努力找出我的代码中的错误。我按照本教程在我的颤振应用程序中实现了 admobs。 https://codelabs.developers.google.com/codelabs/admob-ads-in-flutter#7

我添加了 android manifest 和 infoplist。我还在 main.dart 中初始化了移动广告。

实现代码:

class _NotesRouteState extends State<NotesRoute> {

InterstitialAd? _interstitialAd;
bool _isInterstitialAdReady = false;

void initState() {
    super.initState();
    _loadInterstitialAd();
  }


  void _loadInterstitialAd() {
    InterstitialAd.load(
        adUnitId: AdHelper.interstitialAdUnitId,
      request: AdRequest(),
  adLoadCallback: InterstitialAdLoadCallback(
    onAdLoaded: (ad) {
      this._interstitialAd = ad;
         ad.fullScreenContentCallback = FullScreenContentCallback(
            onAdDismissedFullScreenContent: (ad) {
              print('$ad onAdDismissedFullScreenContent.');
              ad.dispose();
            },
          );
          _isInterstitialAdReady = true;
        },
        onAdFailedToLoad: (err) {
          print('Failed to load an interstitial ad: ${err.message}');
          _isInterstitialAdReady = false;
        },
      ),
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Align(
                alignment: Alignment.topLeft,
                child: Text(
                  'General Subjects',
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 20,
                    color: Colors.lightBlue,
                  ),
                ),
              ),
            ),
            MyButton(
              onTap: () {
                if (_isInterstitialAdReady) {
                  _interstitialAd?.show();
                } else {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => BahasaRoute()),
                  );
                }
              },
           ],
        ),
      ),
    );
  }
}

希望得到大家的指导。谢谢!

【问题讨论】:

标签: flutter admob ads


【解决方案1】:

_isInterstitialAdReady = true 未设置状态。删除 ontap 中的条件检查,它应该可以工作,或者在条件检查之前添加setState()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多