【问题标题】:Flutter App is crashing on launch after implementing google adsFlutter App 在实施谷歌广告后在启动时崩溃
【发布时间】:2021-12-18 11:29:16
【问题描述】:

应用在实施 Google 广告后在启动时崩溃。我正在使用ios模拟器。广告在具有相同代码实现的其他应用程序上运行良好,但在此应用程序上却不行。即使使用原始广告单元 ID,其他应用也会显示测试广告。 插页式广告和奖励广告工作正常,只有横幅广告出现了问题。

错误: 要在此设备上获取测试广告,请设置: Objective-C GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ kGADSimulatorID ]; 迅速 GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ kGADSimulatorID ]

代码: AdHelper 类

import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'dart:io';

class AdHelper {
  //  Android Ad Units
  static String _bannerAd_And = 'ca-app-pub-3884661730977437/3917788070';
  static String _interAd_And = 'ca-app-pub-3884661730977437/1291624734';

  static String _bannerAdTest_And = 'ca-app-pub-3940256099942544/6300978111';
  static String _interAdTest_And = 'ca-app-pub-3940256099942544/1033173712';

  //  iOS Ad Units
  static String _bannerAd_iOS = 'ca-app-pub-3884661730977437/4131225272';
  static String _interAd_iOS = 'ca-app-pub-3884661730977437/6845018522';

  static String _bannerAdTest_iOS = 'ca-app-pub-3940256099942544/2934735716';
  static String _interAdTest_iOS = 'ca-app-pub-3940256099942544/4411468910';

// FN returns Banner AD Unit Id
  static String get bannerAdUnitId {
    if (Platform.isAndroid) {
      return _bannerAdTest_And;
    } else if (Platform.isIOS) {
      return _bannerAdTest_iOS;
    } else {
      throw UnsupportedError('Unsupported platform');
    }
  }

  // FN returns Interstitial Ad Unit Id
  static String get interAdUnitId {
    if (Platform.isAndroid) {
      return _interAdTest_And;
    } else if (Platform.isIOS) {
      return _interAdTest_iOS;
    } else {
      throw UnsupportedError('Unsupported platform');
    }
  }
}

广告控制器

import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'AdMob_Helper.dart';

class AdMobController extends GetxController {
  ///------------------  Init
  @override
  void onInit() {
    getBannerAd();
    super.onInit();
  }

  ///------------------  Dispose
  @override
  void onClose() {
    bannerAd.dispose();
    super.onClose();
  }


  late BannerAd bannerAd;
  bool isBannerLoaded = false;

  //
  void getBannerAd() {
    bannerAd = BannerAd(
      adUnitId: AdHelper.bannerAdUnitId,
      size: AdSize.banner,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (_) {
          isBannerLoaded = true;
          update();
        },
        onAdFailedToLoad: (ad, error) {
          // Releases an ad resource when it fails to load
          ad.dispose();

          print('Ad load failed (code=${error.code} message=${error.message})');
        },
      ),
    );

    // TODO: Load an ad
    bannerAd.load();

    update();
  }
}

【问题讨论】:

    标签: flutter dart admob google-ads-api


    【解决方案1】:

    如果您找到相关链接,请通过此链接, Flutter App Crashes when Trying to Load Interstitial Ad

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多