此广告可能尚未加载或已被处理。找不到具有以下 ID 的广告

问题描述 投票:0回答:1

原生广告是我显示的一个错误。我使用来自 google 的测试广告 ID。 “此广告可能尚未加载或已被处置。找不到具有以下 ID 的广告:1”

enter image description here

您好,我正在使用flutter,我的广告有时会显示,有时会出现此错误,您能帮忙吗,谢谢。 native_ad_widget.darts

class NativeAdWidget extends StatefulWidget {
  const NativeAdWidget({super.key, required this.isDarkMode, required this.isSmallSize});
  final bool isDarkMode;
  final bool isSmallSize;

  @override
  NativeAdWidgetState createState() => NativeAdWidgetState();
}

class NativeAdWidgetState extends State<NativeAdWidget> {
  NativeAd? _nativeAd;
  bool _nativeAdIsLoaded = false;

  final String _adUnitId = AdConfig.getNativeAdUnitId();

  @override
  void initState() {
    loadAd();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final double minHeight = widget.isSmallSize ? 90.0 : 320.0;
    final double maxHeight = widget.isSmallSize ? 120.0 : 360.0;
    return ConstrainedBox(
      constraints: BoxConstraints(
        minWidth: 320.0,
        minHeight: minHeight,
        maxWidth: 400.0,
        maxHeight: maxHeight,
      ),
      child: _nativeAdIsLoaded && _nativeAd != null
          ? AdWidget(ad: _nativeAd!)
          : Container(
              color: Theme.of(context).colorScheme.onBackground,
              alignment: Alignment.center,
              child: const Text(
                'Ad',
                style: TextStyle(fontSize: 20),
              ),
            ),
    );
  }

  // Loads a native ad.
  void loadAd() {
    _nativeAd = NativeAd(
        adUnitId: _adUnitId,
        listener: NativeAdListener(
          onAdLoaded: (ad) {
            debugPrint('$NativeAd loaded.');
            setState(() {
              _nativeAdIsLoaded = true;
            });
          },
          onAdFailedToLoad: (ad, error) {
            // Dispose the ad here to free resources.
            debugPrint('$NativeAd failed to load: $error');
            ad.dispose();
          },
        ),
        request: const AdRequest(),
        nativeAdOptions: NativeAdOptions(
          mediaAspectRatio: MediaAspectRatio.landscape,
          adChoicesPlacement: AdChoicesPlacement.topRightCorner,
        ),

        // Styling
        nativeTemplateStyle: widget.isDarkMode ? nativeTemplateDark() : nativeTemplateLight())
      ..load();
  }

  @override
  void dispose() {
    _nativeAd?.dispose();
    super.dispose();
  }


  //Dark Mode
  NativeTemplateStyle nativeTemplateDark() {
    final templateType = widget.isSmallSize ? TemplateType.small : TemplateType.medium;
    return NativeTemplateStyle(
      templateType: templateType,
      mainBackgroundColor: Colors.grey.shade800,
      cornerRadius: 10.0,
      callToActionTextStyle:
          NativeTemplateTextStyle(textColor: Colors.white, backgroundColor: Config.appThemeColor, style: NativeTemplateFontStyle.normal, size: 16.0),
      primaryTextStyle:
          NativeTemplateTextStyle(textColor: Colors.white, backgroundColor: Colors.transparent, style: NativeTemplateFontStyle.bold, size: 16.0),
      secondaryTextStyle: NativeTemplateTextStyle(
          textColor: Colors.grey.shade100, backgroundColor: Colors.transparent, style: NativeTemplateFontStyle.normal, size: 14.0),
      tertiaryTextStyle: NativeTemplateTextStyle(
          textColor: Colors.grey.shade100, backgroundColor: Colors.transparent, style: NativeTemplateFontStyle.normal, size: 14.0),
    );
  }

  
  // Light Mode
  NativeTemplateStyle nativeTemplateLight() {
    final templateType = widget.isSmallSize ? TemplateType.small : TemplateType.medium;
    return NativeTemplateStyle(
      templateType: templateType,
      mainBackgroundColor: Colors.white,
      cornerRadius: 10.0,
      callToActionTextStyle:
          NativeTemplateTextStyle(textColor: Colors.white, backgroundColor: Config.appThemeColor, style: NativeTemplateFontStyle.normal, size: 16.0),
      primaryTextStyle: NativeTemplateTextStyle(
          textColor: Colors.grey.shade900, backgroundColor: Colors.transparent, style: NativeTemplateFontStyle.bold, size: 16.0),
      secondaryTextStyle: NativeTemplateTextStyle(
          textColor: Colors.blueGrey.shade600, backgroundColor: Colors.transparent, style: NativeTemplateFontStyle.normal, size: 14.0),
      tertiaryTextStyle: NativeTemplateTextStyle(
          textColor: Colors.blueGrey.shade500, backgroundColor: Colors.transparent, style: NativeTemplateFontStyle.normal, size: 14.0),
    );
  }
}
flutter dart
1个回答
0
投票

正确答案可以在这里找到:

找不到具有以下 ID 的广告。 Natiev 广告颤动 google_mobile_ads

我也遇到了同样的问题。尽管问题出在广告尺寸不同,但链接上的解决方案效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.