loadNativeWithLayout static method

void loadNativeWithLayout(
  1. BuildContext context,
  2. NativeHolder nativeHolder,
  3. NativeAdmobType nativeType, {
  4. required dynamic onAdLoaded(
    1. String
    ),
})

Implementation

static void loadNativeWithLayout(BuildContext context,
    NativeHolder nativeHolder, NativeAdmobType nativeType,
    {required Function(String) onAdLoaded}) async {
  if (!_isShowAds || await isNetworkConnected() == false) {
    nativeHolder.isLoad = false;
    nativeHolder.adsNativeController.value = Pair(null, "no internet");
    return;
  }

  final String adUnitId = _isDebug
      ? _idTestNativeAd
      : Platform.isAndroid
          ? nativeHolder.idAndroid
          : nativeHolder.idIOS;

  if (!context.mounted) {
    return;
  }

  if (nativeHolder.adsNativeController.value.nativeAd != null) {
    log("message native not null");
    return;
  }

  nativeHolder.isLoad = true;
  nativeHolder.adsNativeController.value = Pair(null, "");

  NativeAd(
    adUnitId: adUnitId,
    factoryId: (nativeType == NativeAdmobType.NATIVE_MEDIUM)
        ? 'NativeCustomMedium'
        : 'NativeCustomSmall',
    listener: NativeAdListener(
      onAdLoaded: (ad) async {
        var result = await methodChannel.invokeMethod(
            ((nativeType == NativeAdmobType.NATIVE_MEDIUM)
                ? method_medium
                : method_small));
        log("TAG===== headline $result");
        nativeHolder.adsNativeController.value =
            Pair(ad as NativeAd, "success");
        onAdLoaded(result);
      },
      onAdFailedToLoad: (ad, error) {
        nativeHolder.isLoad = false;
        nativeHolder.adsNativeController.value =
            Pair(ad as NativeAd, "error");
        ad.dispose();
      },
      // Called when a click is recorded for a NativeAd.
      onAdClicked: (ad) {},
      // Called when an impression occurs on the ad.
      onAdImpression: (ad) {},
      // Called when an ad removes an overlay that covers the screen.
      onAdClosed: (ad) {},
      // Called when an ad opens an overlay that covers the screen.
      onAdOpened: (ad) {},
      // For iOS only. Called before dismissing a full screen view
      onAdWillDismissScreen: (ad) {},
      // Called when an ad receives revenue value.
      onPaidEvent: (ad, valueMicros, precision, currencyCode) {},
    ),
    request: const AdRequest(),
  ).load();
}