load method
Loads an ad if the gate allows and none is loaded/loading.
Implementation
@override
Future<void> load() async {
if (_disposed || _retired) return;
if (_state.value is AdLoading ||
_state.value is AdLoaded ||
_state.value is AdShowing) {
return;
}
// Set Loading synchronously, before any await, so a concurrent load()
// call sees it immediately and bails instead of racing to a second
// in-flight loadHandle() call (each would overwrite _handle, leaking
// the loser's ad).
_state.value = const AdLoading();
// ONE try around the whole async body — the gate await included. The gate
// itself no longer throws, but the rule stands regardless of collaborator
// promises: from the moment AdLoading is written, ANY escape path that is
// not a state write + timer arm leaves the slot wedged forever (4.0
// audit; the gate await used to sit outside this try).
try {
final blocked = await _gate.loadBlockReason(slot);
if (_disposed || _retired) return;
if (blocked != null) {
noteBlocked(blocked);
// A refused load is a STATE, not a side channel (3.0): consent still
// pending, Remove-Ads on and "nothing requested yet" used to be
// indistinguishable AdIdle.
_state.value = AdBlocked(blocked);
_scheduleGateRecheck();
return;
}
// Capture the consent generation this request is dispatched under, to
// detect a consent mutation that lands WHILE the request is in flight
// (release gate #2 — the mid-load window): the gate already passed, so
// it is dropped-and-reloaded on completion below rather than by
// recheckGate (which handles only the already-warm case).
final requestGeneration = _gate.consentGeneration;
// Watchdog: the plugin has no load timeout of its own — a callback that
// never arrives must fail this attempt (and dispose its late handle if
// one ever shows up) instead of pinning the slot at AdLoading (I-C).
final handle = await watchAdLoad(
pending: loadHandle(),
timeout: _retry.loadTimeout,
disposeLate: (late) => late.dispose(),
slot: slot,
);
if (_disposed || _retired) {
safeUnawaited(handle.dispose(), debugName: 'handle');
return;
}
if (_gate.consentGeneration != requestGeneration) {
// Consent mutated while this ad was in flight — it was requested under
// the OLD consent. Drop it unshown and reload through the fresh gate,
// so it never records a stale-consent impression.
safeUnawaited(handle.dispose(), debugName: 'handle');
_state.value = const AdIdle();
unawaited(load());
return;
}
if (!_gate.isEnabled) {
// disableAds() / graph-dispose landed WHILE this ad was in flight: the
// gate passed at request time, but ads are no longer enabled.
// recheckGate cannot drop an AdLoading controller, so drop the
// freshly-loaded handle HERE — synchronously, in the same turn as the
// AdLoaded write below — so no warm full-screen inventory is ever
// retained (or externally reported ready) under Remove-Ads (5.1.2
// in-flight-load race). enableAds() re-warms via the gate recheck. A
// pure bool read: no transient internalError can wrongly drop good
// inventory (consent-staleness is the generation check above).
safeUnawaited(handle.dispose(), debugName: 'handle');
noteBlocked(AdBlockReason.adsDisabled);
_state.value = const AdBlocked(AdBlockReason.adsDisabled);
_scheduleGateRecheck();
return;
}
_handle = handle;
_contentSub = handle.contentEvents.listen(_onContentEvent);
_paidSub = handle.paidEvents.listen(_dispatchPaid);
_loadedAt = _now();
_loadedGeneration = requestGeneration;
_attempts = 0;
_gateAttempts = 0;
_lastBlockReason = null;
_state.value = const AdLoaded();
_scheduleExpiry();
} catch (e) {
// Catch EVERYTHING, not just AdFlowError. The seam's contract says it
// throws AdFlowError, but a real platform channel violates that freely:
// MissingPluginException, PlatformException, or the plugin's own
// canRequestAds() force-unwrapping a null channel result. Before this,
// such a throw escaped `on AdFlowError`, left the controller pinned at
// AdLoading (which load()'s own re-entry guard then rejects forever) and
// armed no retry — one platform hiccup killed the slot for the session.
if (_disposed) return;
_state.value = AdFailed(asAdFlowError(e, AdFlowErrorKind.loadFailed));
_scheduleRetry();
}
}