Skip to content

Commit 7f88feb

Browse files
committed
fix(ads): ensure ad widgets rebuild for new ads
- Add ValueKey to _AdDispatcher in AdFeedItemWidget to trigger rebuilds when adFeedItem changes - Update _AdDispatcher constructor to include super.key for proper key handling - Improve comments to explain ad rendering lifecycle and widget rebuilding process
1 parent b608338 commit 7f88feb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/ads/widgets/ad_feed_item_widget.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ class AdFeedItemWidget extends StatelessWidget {
3737
),
3838
// The _AdDispatcher is responsible for selecting the correct
3939
// provider-specific widget.
40-
child: _AdDispatcher(nativeAd: adFeedItem.nativeAd),
40+
// Use a ValueKey to ensure that when the adFeedItem changes (e.g., a new
41+
// ad is loaded for the same slot), the _AdDispatcher and its child
42+
// (AdmobNativeAdWidget) are rebuilt, triggering the new ad's lifecycle.
43+
child: _AdDispatcher(
44+
key: ValueKey(adFeedItem.id), // Use adFeedItem.id as the key
45+
nativeAd: adFeedItem.nativeAd,
46+
),
4147
),
4248
);
4349
}
@@ -46,7 +52,7 @@ class AdFeedItemWidget extends StatelessWidget {
4652
/// A private helper widget that selects the correct ad rendering widget
4753
/// based on the [NativeAd.provider].
4854
class _AdDispatcher extends StatelessWidget {
49-
const _AdDispatcher({required this.nativeAd});
55+
const _AdDispatcher({required this.nativeAd, super.key}); // Add super.key
5056

5157
final NativeAd nativeAd;
5258

@@ -57,6 +63,8 @@ class _AdDispatcher extends StatelessWidget {
5763
switch (nativeAd.provider) {
5864
case AdProviderType.admob:
5965
// If the provider is AdMob, render the AdmobNativeAdWidget.
66+
// Pass the nativeAd object directly. The AdmobNativeAdWidget is now
67+
// stateful and will manage its own lifecycle.
6068
return AdmobNativeAdWidget(nativeAd: nativeAd);
6169
case AdProviderType.placeholder:
6270
// If the provider is a placeholder, render the PlaceholderAdWidget.

0 commit comments

Comments
 (0)