我正在使用这个插件
此插件中的 Admob factoryId 是什么..
我从哪里获得 admob 广告的factoryId?
因为当我尝试调用原生广告时,我收到此错误
E/flutter (19354): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(NativeAdError, Can't find NativeAdFactory with id: FLTGoogleMobileAdsPlugin, null, null)
E/flutter (19354): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:581
E/flutter (19354): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:158
E/flutter (19354): <asynchronous suspension>
E/flutter (19354): #2 NativeAd.load
package:google_mobile_ads/src/ad_containers.dart:576
E/flutter (19354): <asynchronous suspension>
E/flutter (19354):
这是我的原生广告类
class NativeAdWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => NativeAdState();
}
class NativeAdState extends State<NativeAdWidget> {
NativeAd _nativeAd;
final Completer<NativeAd> nativeAdCompleter = Completer<NativeAd>();
@override
void initState() {
super.initState();
_nativeAd = NativeAd(
adUnitId: NativeAd.testAdUnitId,
request: AdRequest(),
factoryId: 'adFactoryExample',
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$NativeAd loaded.');
nativeAdCompleter.complete(ad as NativeAd);
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
print('$NativeAd failedToLoad: $error');
nativeAdCompleter.completeError(null);
},
onAdOpened: (Ad ad) => print('$NativeAd onAdOpened.'),
onAdClosed: (Ad ad) => print('$NativeAd onAdClosed.'),
onApplicationExit: (Ad ad) => print('$NativeAd onApplicationExit.'),
),
);
Future<void>.delayed(Duration(seconds: 1), () => _nativeAd?.load());
}
@override
void dispose() {
super.dispose();
_nativeAd?.dispose();
_nativeAd = null;
}
@override
Widget build(BuildContext context) {
return FutureBuilder<NativeAd>(
future: nativeAdCompleter.future,
builder: (BuildContext context, AsyncSnapshot<NativeAd> snapshot) {
Widget child;
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
child = Container();
break;
case ConnectionState.done:
if (snapshot.hasData) {
child = AdWidget(ad: _nativeAd);
} else {
child = Text('Error loading $NativeAd');
}
}
return Container(
width: 250,
height: 350,
child: child,
color: Colors.blueGrey,
);
},
);
}
}
我这样称呼原生广告
adWidget = NativeAdWidget(),
也许我收到此错误是因为我的factoryid 为空?
我使用了这个插件的横幅广告,效果很好..
工厂id是您在MainActivity文件中配置或注册的id,java或kotiln,它代表您的自定义
NativeAdFactory
类
如果您查看评论中也提供的admob代码实验室,您会看到他们在这里注册了factoryId
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
// TODO: Register the ListTileNativeAdFactory
GoogleMobileAdsPlugin.registerNativeAdFactory(
flutterEngine, "listTile", ListTileNativeAdFactory(context))
}
...
}
然后他们在清理这里时取消注册
class MainActivity: FlutterActivity() {
...
override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
super.cleanUpFlutterEngine(flutterEngine)
// TODO: Unregister the ListTileNativeAdFactory
GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "listTile")
}
}
然后你在这里的flutter代码上使用factoryId
listTile
adUnitId: AdHelper.nativeAdUnitId,
factoryId: 'listTile',
request: AdRequest(),
listener: NativeAdListener(
onAdLoaded: (ad) {
setState(() {
_ad = ad as NativeAd;
});
},
onAdFailedToLoad: (ad, error) {
// Releases an ad resource when it fails to load
ad.dispose();
print('Ad load failed (code=${error.code} message=${error.message})'); },
),
);
_ad.load();
因此请确保在您的
adFactoryExample
文件上注册了factoryId MainActivity
,并创建一个继承GoogleMobileAdsPlugin.NativeAdFactory
的类
希望对大家有帮助。
您没有完成原生广告伙伴的所有设置部分。您还需要在JAVA中进行一些设置。
根据文档:
平台设置
原生广告通过平台原生的 UI 组件呈现给用户。 (例如 Android 上的视图或 iOS 上的 UIView)。由于原生广告需要原生的 UI 组件 平台,此功能需要针对 Android 和 iOS 进行额外设置。
您可以在插件文档本身中找到所有内容https://pub.dev/packages/google_mobile_ads