首页 > 解决方案 > iOS 14 在 Flutter 上使用带有 google admob 的横幅广告时崩溃

问题描述

我想这很难说是不可能的,但我还是想试一试。我刚刚更改为在我的 Flutter 应用程序中使用google_mobile_ads库来显示横幅广告。它工作正常,尽管我在列表中显示这些时确实看到了一些性能问题。我认为这与横幅渲染有关,我可以想象它非常慢。

无论如何,在 iOS 上,我在多次上下滚动列表后出现崩溃,从日志中看起来它可能与广告库有关。

我正在iPhone OS 14.4.2 (18D70)运行iPhone8,1. 这是我得到的日志:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Application Specific Information:
abort() called

Last Exception Backtrace:
0   CoreFoundation                  0x1a08b19d8 __exceptionPreprocess + 216
1   libobjc.A.dylib                 0x1b4c34b54 objc_exception_throw + 55
2   CoreFoundation                  0x1a091bd98 _CFThrowFormattedException + 111
3   CoreFoundation                  0x1a09271f4 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:].cold.4 + 47
4   CoreFoundation                  0x1a07ac8a8 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 239
5   CoreFoundation                  0x1a079fc04 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 55
6   Runner                          0x1006d7688 -[FLTAdInstanceManager onAdLoaded:responseInfo:] + 2340488 (FLTAdInstanceManager_Internal.m:82)
7   Runner                          0x1006d5268 -[FLTBannerAd bannerViewDidReceiveAd:] + 2331240 (FLTAd_Internal.m:207)
8   Runner                          0x1005ef224 GAD_GADBannerView_arm64_8_5_0 + 5139
9   Runner                          0x1006433f8 __destroy_helper_block_e8_32s40s48s56s64s72w + 159

不确定这是否与我加载广告的方式有关,但我几乎遵循了官方 codelabs上的示例。它看起来像这样:


void initState() {
  super.initState();

  final adUnit = DebugPlaceholder.isDebugMode
    ? debugAdUnit
    : liveAdUnit;

  _ad = BannerAd(
      adUnitId: adUnit,
      size: AdSize.largeBanner,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (_) {
          setState(() {
            _adLoaded = true;
          });
        },
        onAdFailedToLoad: (ad, error) {
          _ad.dispose();
          print('Ad load failed (code=${error.code} message=${error.message})');
        }
      )
  );

  _ad.load();
}

@override
void dispose() {
  _ad.dispose();
  super.dispose();
}

@override
Widget build(BuildContext context) {
  if (!_adLoaded) return Container();

  return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: _ad.size.height.toDouble(),
      width: _ad.size.width.toDouble(),
      child: AdWidget(ad: _ad)
  );
}

任何提示,即使不是关于实际问题,而是关于如何解决它,非常感谢!

标签: iosflutteradmob

解决方案


推荐阅读