load method

Future<bool> load({
  1. String? unitId,
  2. NativeAdOptions? options,
  3. bool force = false,
  4. Duration? timeout,
  5. bool? nonPersonalizedAds,
  6. List<String> keywords = const [],
})
override

Load the ad. If the controller is disposed or the Mobile Ads SDK (ADMOB SDK) is not initialized, an AssertionError is thrown.

For more info, read the documentation

Implementation

Future<bool> load({
  /// The ad unit id. If null, uses [MobileAds.nativeAdUnitId]
  String? unitId,
  NativeAdOptions? options,

  /// Force to load an ad even if another is already avaiable
  bool force = false,

  /// The timeout of this ad. If null, defaults to 1 minute
  Duration? timeout,

  /// Whether non-personalized ads (ads that are not based on a user’s past behavior) should be enabled.
  bool? nonPersonalizedAds,

  /// {@macro ads.keywords}
  List<String> keywords = const [],
}) async {
  ensureAdNotDisposed();
  assertMobileAdsIsInitialized();
  if (!debugCheckAdWillReload(isLoaded, force)) return false;
  unitId ??= MobileAds.nativeAdUnitId ?? MobileAds.nativeAdTestUnitId;
  isLoaded = (await channel.invokeMethod<bool>('loadAd', {
    'unitId': unitId,
    'options': (options ?? NativeAdOptions()).toJson(),
    'nonPersonalizedAds': nonPersonalizedAds ?? this.nonPersonalizedAds,
    'keywords': keywords,
  }).timeout(
    timeout ?? this.loadTimeout,
    onTimeout: () {
      if (!onEventController.isClosed && !isLoaded)
        onEventController.add({
          NativeAdEvent.loadFailed: AdError.timeoutError,
        });
      return false;
    },
  ))!;
  if (isLoaded) lastLoadedTime = DateTime.now();
  return isLoaded;
}