load method

Future<void> load()

Preloads an interstitial ad.

Automatically retries with exponential backoff on failure. Does nothing if an ad is already loaded or loading.

Implementation

Future<void> load() async {
  if (_isDisposed) return;
  if (_state == AdState.loading || _state == AdState.ready) return;
  _state = AdState.loading;

  final resolvedId = adUnitId.resolved(adType: AdType.interstitial);

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

  if (_isDisposed) return;

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