load method

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

Load the ad. Shows a warning if the ad is already loaded

Returns true if the ad was loaded successfully or false if an error happened

For more info, read the documentation

Implementation

Future<bool> load({
  /// The ad unit id. If null, [MobileAds.appOpenAdUnitId] is used
  String? unitId,

  /// The orientation. Avaiable orientations:\
  /// 1 - [ORIENTATION_PORTRAIT]\
  /// 2 - [ORIENTATION_LANDSCAPE]\
  ///
  /// If null, defaults to the current device orientation
  int? orientation,

  /// 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 should be enabled
  bool? nonPersonalizedAds,

  /// The keywords of the ad
  List<String> keywords = const [],
}) async {
  ensureAdNotDisposed();
  assertMobileAdsIsInitialized();
  if (!debugCheckAdWillReload(isLoaded, force)) return false;
  if (orientation != null) {
    assert(
      [ORIENTATION_PORTRAIT, ORIENTATION_LANDSCAPE].contains(orientation),
      'The orientation must be a valid orientation: $ORIENTATION_PORTRAIT, $ORIENTATION_LANDSCAPE',
    );
  } else {
    final window = WidgetsBinding.instance!.window;
    final size = window.physicalSize / window.devicePixelRatio;
    final deviceOrientation = size.width > size.height
        ? Orientation.landscape
        : Orientation.portrait;

    switch (deviceOrientation) {
      case Orientation.landscape:
        orientation = ORIENTATION_LANDSCAPE;
        break;
      case Orientation.portrait:
        orientation = ORIENTATION_PORTRAIT;
        break;
    }
  }
  final bool loaded = (await channel.invokeMethod<bool>('loadAd', {
    'unitId': unitId ??
        this.unitId ??
        MobileAds.appOpenAdUnitId ??
        MobileAds.appOpenAdTestUnitId,
    'orientation': orientation,
    'nonPersonalizedAds': nonPersonalizedAds ?? this.nonPersonalizedAds,
    'keywords': keywords,
  }).timeout(
    timeout ?? this.loadTimeout,
    onTimeout: () {
      if (!onEventController.isClosed && !isLoaded)
        onEventController.add({
          FullScreenAdEvent.loadFailed: AdError.timeoutError,
        });
      return false;
    },
  ))!;
  if (loaded) lastLoadedTime = DateTime.now();
  return loaded;
}