loadRewarded method

Future<void> loadRewarded()

Preloads a rewarded ad.

Does nothing if AdConfig.rewardedId is null. Automatically retries with exponential backoff on failure.

Throws StateError if not initialized.

Implementation

Future<void> loadRewarded() async {
  _assertInitialized();
  if (_config.rewardedId == null) return;
  if (_rewardedState == AdState.loading ||
      _rewardedState == AdState.ready) {
    return;
  }
  if (_activeAdCount >= maxConcurrentAds) return;

  _rewardedState = AdState.loading;
  _activeAdCount++;

  final adUnitId = _config.rewardedId!.resolved(
    adType: AdType.rewarded,
  );

  final loaded = await _retryStrategy.execute<bool>(
    action: () async {
      // In production, this would call RewardedAd.load()
      return true;
    },
    onRetry: (attempt, error) {
      // Log retry attempt
    },
  );

  if (loaded == true) {
    _rewardedState = AdState.ready;
    emitEvent(AdEvent(type: AdEventType.loaded, adUnitId: adUnitId));
  } else {
    _rewardedState = AdState.error;
    _activeAdCount--;
    emitEvent(AdEvent(
      type: AdEventType.failedToLoad,
      adUnitId: adUnitId,
      error: const AdError(code: 0, message: 'Failed to load after retries'),
    ));
  }
}