load method

  1. @override
Future<bool> load()
override

Implementation

@override
Future<bool> load() {
  if (state == AdState.loaded && _ad != null) return Future.value(true);
  final pending = _loadFuture;
  if (pending != null) return pending;

  state = AdState.loading;
  _isPreloading = true;
  final completer = Completer<bool>();
  _loadFuture = completer.future;

  RewardedAd.load(
    adUnitId: adUnitId,
    request: const AdRequest(),
    rewardedAdLoadCallback: RewardedAdLoadCallback(
      onAdLoaded: (ad) {
        _ad = ad;
        state = AdState.loaded;
        _isPreloading = false;
        completer.complete(true);
      },
      onAdFailedToLoad: (error) {
        state = AdState.failed;
        _isPreloading = false;
        dPrint('❌ Rewarded load failed: ${error.message}');
        completer.complete(false);
      },
    ),
  );

  final future = completer.future;
  future.whenComplete(() {
    if (identical(_loadFuture, future)) _loadFuture = null;
  });
  return future;
}