Skip to content

Commit 09b9285

Browse files
committed
fix(ads): prevent crashes in feed ad loader widget
- Complete AsyncCompleter normally instead of with errors - Fixes potential race conditions and crashes when: - Widget is updated with new ID or config - Widget is disposed - No ad identifier is returned - No ad is returned - Errors occur during ad loading
1 parent b55a504 commit 09b9285

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

lib/ads/widgets/feed_ad_loader_widget.dart

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
9999
// completed. This prevents a race condition if a new load is triggered
100100
// while an old one is still in progress.
101101
if (_loadAdCompleter != null && !_loadAdCompleter!.isCompleted) {
102-
_loadAdCompleter?.completeError(
103-
StateError(
104-
'Ad loading cancelled: Widget updated with new ID or config.',
105-
),
106-
);
102+
_loadAdCompleter!.complete(); // Complete normally to prevent crashes
107103
}
108104
_loadAdCompleter = null;
109105

@@ -128,9 +124,7 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
128124
// This prevents `setState()` calls on a disposed widget.
129125
// Ensure the completer is not already completed before attempting to complete it.
130126
if (_loadAdCompleter != null && !_loadAdCompleter!.isCompleted) {
131-
_loadAdCompleter?.completeError(
132-
StateError('Ad loading cancelled: Widget disposed.'),
133-
);
127+
_loadAdCompleter!.complete(); // Complete normally to prevent crashes
134128
}
135129
_loadAdCompleter = null;
136130
super.dispose();
@@ -189,10 +183,10 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
189183
_hasError = true;
190184
_isLoading = false;
191185
});
186+
// Complete the completer normally, indicating that loading finished
187+
// but no ad was available. This prevents crashes.
192188
if (_loadAdCompleter?.isCompleted == false) {
193-
_loadAdCompleter?.completeError(
194-
StateError('Failed to load ad: No ad identifier.'),
195-
);
189+
_loadAdCompleter!.complete();
196190
}
197191
return;
198192
}
@@ -240,11 +234,10 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
240234
_hasError = true;
241235
_isLoading = false;
242236
});
243-
// Complete the completer with an error only if it hasn't been completed already.
237+
// Complete the completer normally, indicating that loading finished
238+
// but no ad was available. This prevents crashes.
244239
if (_loadAdCompleter?.isCompleted == false) {
245-
_loadAdCompleter?.completeError(
246-
StateError('Failed to load ad: No ad returned.'),
247-
);
240+
_loadAdCompleter!.complete();
248241
}
249242
}
250243
} catch (e, s) {
@@ -259,9 +252,10 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
259252
_hasError = true;
260253
_isLoading = false;
261254
});
262-
// Complete the completer with an error only if it hasn't been completed already.
255+
// Complete the completer normally, indicating that loading finished
256+
// but an error occurred. This prevents crashes.
263257
if (_loadAdCompleter?.isCompleted == false) {
264-
_loadAdCompleter?.completeError(e);
258+
_loadAdCompleter!.complete();
265259
}
266260
}
267261
}

0 commit comments

Comments
 (0)