loadAd method

Future<String?> loadAd({
  1. List<String>? adUnitIds,
  2. String? adUnitId,
  3. String? country,
})

Loads a RewardedAd.

This method loads a rewarded ad with the provided ad unit ID and sets up the full screen content callbacks to handle ad display and events.

Implementation

Future<String?> loadAd({
  List<String>? adUnitIds,
  String? adUnitId,
  String? country,
}) async {
  country ??= userCountry;
  _rewardedAd = null;

  // Prioritize single adUnitId if provided
  if (adUnitId != null) {
    _logger.debug('Loading rewarded ad from ad unit ID.');
    final requestId = await _requestAd(adUnitId, country);

    if (requestId != null) return requestId;
  }

  // Otherwise, load from adUnitIds
  if (adUnitIds != null) {
    _logger.debug('Loading rewarded ad from ad unit IDs.');
    final requestId = await _requestAds(adUnitIds, country);

    if (requestId != null) return requestId;
  }

  // Fallback to adInfo if no adUnitId or adUnitIds provided
  if (rewardedAdUnitIds != null) {
    _logger.debug('Loading rewarded ad from default ad unit IDs.');
    final requestId = await _requestAds(rewardedAdUnitIds!, country);

    if (requestId != null) return requestId;
  }

  // Fallback to adUnitId if no adUnitIds provided
  if (rewardedAdUnitId != null) {
    _logger.debug('Loading rewarded ad from default ad unit ID.');
    final requestId = await _requestAd(rewardedAdUnitId!, country);

    if (requestId != null) return requestId;
  }

  _logger.warning('No ad unit ID provided for RewardedAd.');

  return null;
}