showRewarded method

Future<AdReward?> showRewarded()

Shows a preloaded rewarded ad and returns the reward.

Returns AdReward if the user watched to completion, or null if skipped, cancelled, or not loaded.

Implementation

Future<AdReward?> showRewarded() async {
  _assertInitialized();
  if (_config.rewardedId == null) return null;
  if (_rewardedState != AdState.ready) return null;

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

  // In production, this would call rewardedAd.show() and wait
  // for the onUserEarnedReward callback.
  // For the library's internal logic, we simulate the reward flow.
  const reward = AdReward(amount: 1, type: 'reward');
  emitEvent(AdEvent(type: AdEventType.shown, adUnitId: adUnitId));
  emitEvent(AdEvent(
    type: AdEventType.rewardEarned,
    adUnitId: adUnitId,
    reward: reward,
  ));

  _rewardedState = AdState.idle;
  _activeAdCount--;

  // Auto-preload next
  unawaited(loadRewarded());
  return reward;
}