Skip to content

Commit b55a504

Browse files
committed
fix(ads): reimplement AdMob ad object disposal in AdmobInlineAdWidget
- Restore proper ad object disposal when the widget is disposed or updated - Remove reliance on InlineAdCacheService for ad object lifecycle management - Add _disposeCurrentAd method to handle ad disposal - Update log messages to reflect changes in ad object management
1 parent 2c57d60 commit b55a504

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/ads/widgets/admob_inline_ad_widget.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,20 @@ class _AdmobInlineAdWidgetState extends State<AdmobInlineAdWidget> {
5656
void didUpdateWidget(covariant AdmobInlineAdWidget oldWidget) {
5757
super.didUpdateWidget(oldWidget);
5858
// If the inlineAd object itself has changed (e.g., a new ad was loaded
59-
// for the same placeholder ID), set the new one.
60-
// The disposal of the old ad is now handled by the InlineAdCacheService when
61-
// the cache is cleared, preventing premature disposal of cached ads.
59+
// for the same placeholder ID), dispose the old ad and set the new one.
6260
if (widget.inlineAd.id != oldWidget.inlineAd.id) {
61+
_disposeCurrentAd(); // Dispose the old ad object
6362
_setAd();
6463
}
6564
}
6665

6766
@override
6867
void dispose() {
69-
// The AdMob ad object is no longer disposed here.
70-
// Its lifecycle is managed by the InlineAdCacheService to prevent crashes
71-
// when the widget is scrolled out of view and then back in.
72-
// The ad will be disposed when InlineAdCacheService.clearAllAds() is called,
73-
// typically on a full feed refresh.
74-
_logger.info(
75-
'AdmobInlineAdWidget disposed. Ad object lifecycle managed by InlineAdCacheService.',
76-
);
68+
// Dispose the AdMob ad object when the widget is removed from the tree.
69+
// This is crucial to prevent "AdWidget is already in the Widget tree" errors
70+
// and memory leaks, as each AdWidget instance should manage its own ad object.
71+
_disposeCurrentAd();
72+
_logger.info('AdmobInlineAdWidget disposed. Ad object explicitly disposed.');
7773
super.dispose();
7874
}
7975

@@ -88,7 +84,6 @@ class _AdmobInlineAdWidgetState extends State<AdmobInlineAdWidget> {
8884
_ad = widget.inlineAd.adObject as admob.BannerAd;
8985
} else {
9086
_ad = null;
91-
9287
_logger.severe(
9388
'The provided ad object for AdMob inline ad is not of type '
9489
'admob.NativeAd or admob.BannerAd. Received: '
@@ -97,6 +92,15 @@ class _AdmobInlineAdWidgetState extends State<AdmobInlineAdWidget> {
9792
}
9893
}
9994

95+
/// Disposes the currently held [_ad] object if it's an [admob.Ad].
96+
void _disposeCurrentAd() {
97+
if (_ad is admob.Ad) {
98+
_logger.info('Disposing AdMob ad object: ${_ad!.adUnitId}');
99+
_ad!.dispose();
100+
_ad = null;
101+
}
102+
}
103+
100104
@override
101105
Widget build(BuildContext context) {
102106
if (_ad == null) {

0 commit comments

Comments
 (0)