Flutter中Native Ad无法加载问题求助
Flutter原生广告加载失败求助
在Flutter中尝试加载原生广告(Native Ad)但始终失败,尝试多种方法仍未解决。以下是实现代码及调试控制台输出,请求协助排查问题:
实现代码
import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'dart:io'; import 'package:flutter/material.dart'; class NativeExample extends StatefulWidget { const NativeExample({super.key}); @override NativeExampleState createState() => NativeExampleState(); } class NativeExampleState extends State<NativeExample> { NativeAd? _nativeAd; bool _nativeAdIsLoaded = false; String? _versionString; // final double _adAspectRatioSmall = (91 / 355); final double _adAspectRatioMedium = (370 / 355); final String _adUnitId = 'ca-app-pub-3940256099942544/2247696110'; @override void initState() { super.initState(); MobileAds.instance.updateRequestConfiguration(RequestConfiguration( testDeviceIds: ['582750308C87F9B58075658F06F52379'])); loadAd(); _loadVersionString(); } 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(), // Styling nativeTemplateStyle: NativeTemplateStyle( // Required: Choose a template. templateType: TemplateType.medium, // Optional: Customize the ad's style. mainBackgroundColor: Colors.purple, cornerRadius: 10.0, callToActionTextStyle: NativeTemplateTextStyle( textColor: Colors.cyan, backgroundColor: Colors.red, style: NativeTemplateFontStyle.monospace, size: 16.0), primaryTextStyle: NativeTemplateTextStyle( textColor: Colors.red, backgroundColor: Colors.cyan, style: NativeTemplateFontStyle.italic, size: 16.0), secondaryTextStyle: NativeTemplateTextStyle( textColor: Colors.green, backgroundColor: Colors.black, style: NativeTemplateFontStyle.bold, size: 16.0), tertiaryTextStyle: NativeTemplateTextStyle( textColor: Colors.brown, backgroundColor: Colors.amber, style: NativeTemplateFontStyle.normal, size: 16.0))) ..load(); } void _loadVersionString() { MobileAds.instance.getVersionString().then((value) { setState(() { _versionString = value; }); }); } @override void dispose() { _nativeAd?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return MaterialApp( title: 'Native Example', home: Scaffold( appBar: AppBar( title: const Text('Native Example'), ), body: SizedBox( height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, child: Column( children: [ Stack( children: [ SizedBox( height: MediaQuery.of(context).size.width * _adAspectRatioMedium, width: MediaQuery.of(context).size.width, ), if (_nativeAdIsLoaded && _nativeAd != null) AdWidget(ad: _nativeAd!), ], ), TextButton( onPressed: loadAd, child: const Text("Refresh Ad"), ), if (_versionString != null) Text(_versionString!) ], ), ), ), ); } /// Loads a native ad. }
调试控制台输出
Restarted application in 3,656ms. D/DynamitePackage( 6642): Instantiating com.google.android.gms.ads.ChimeraAdLoaderBuilderCreatorImpl I/Ads ( 6642): This request is sent from a test device. W/ConnectionStatusConfig( 6642): Dynamic lookup for intent failed for action: com.google.android.gms.ads.service.CACHE W/ConnectionStatusConfig( 6642): Dynamic lookup for intent failed for action: com.google.android.gms.ads.service.START I/Ads ( 6642): Received log message: <Google:HTML> 原生广告响应不正确。点击操作未正确指定 I/Ads ( 6642): Ad failed to load : 0 I/flutter ( 6642): NativeAd failed to load: LoadAdError(code: 0, domain: com.google.android.gms.ads, message: 内部错误., responseInfo: ResponseInfo(responseId: CNqw1eju0YIDFeMWgwMdqJAE9w, mediationAdapterClassName: , adapterResponses: [AdapterResponseInfo(adapterClassName: com.google.ads.mediation.admob.AdMobAdapter, latencyMillis: 47, description: { I/flutter ( 6642): "Adapter": "com.google.ads.mediation.admob.AdMobAdapter", I/flutter ( 6642): "Latency": 47, I/flutter ( 6642): "Ad Source Name": "Reservation campaign", I/flutter ( 6642): "Ad Source ID": "7068401028668408324", I/flutter ( 6642): "Ad Source Instance Name": "[DevRel] [DO NOT EDIT] Native Ads Campaign", I/flutter ( 6642): "Ad Source Instance ID": "3518433842871043", I/flutter ( 6642): "Credentials": {}, I/flutter ( 6642): "Ad Error": { I/flutter ( 6642): "Code": 0, I/flutter ( 6642): "Message": "内部错误.", I/flutter ( 6642): "Domain": "com.google.android.gms.ads", I/flutter ( 6642): "Cause": "null" I/flutter ( 6642): } I/flutter ( 6642): }, adUnitMapping: {}, adError: AdError(code: 0, domain: com.google.android.gms.ads, message: 内部错误.), adSourceName: Reservation campaign, adSourceId: 7068401028668408324, adSourceInstanceName: [DevRel] [DO NOT EDIT] Native Ads Campaign, adSourceInstanceId: 3518433842871043)], loadedAdapterResponseInfo: null), responseExtras: {mediation_group_name: Campaign}) D/EGL_emulation( 6642): app_time_stats: avg=112665.77ms min=112665.77ms max=112665.77ms count=1
关键错误提示
return (<Google:HTML> 原生广告响应不正确。点击操作未正确指定 )
最终返回广告加载失败错误码0(内部错误)。
内容的提问来源于stack exchange,提问作者MD Samm
相关产品推荐
相关产品推荐

