Flutter - What is factoryId for Admob in Flutter?

Viewed 1408

I am Using This plugin

What is factoryId For Admob in this plugin..

and where i get my factoryId for admob ads ?

because when i try to call native ad i get this error

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):

this is my native ad class


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,
        );
      },
    );
  }
}

I call native ads like this

adWidget = NativeAdWidget(),

Maybe i get this error because my factoryid is null?

i used banner ad of this plugin that working fine..

enter image description here

1 Answers

You didn't do all the setup part of Native ads mate. You also need to do some setup in JAVA.

According to the docs:

Platform Setup
Native Ads are presented to users via UI components that are native to the platform. (e.g. A View on Android or a UIView on iOS). Since Native Ads require UI components native to a platform, this feature requires additional setup for Android and iOS.

You can find everything in the plugin docs itself https://pub.dev/packages/google_mobile_ads

Related