@@ -23,6 +23,7 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
23
23
final Map <BannerAd , int > _bannerPositions = {};
24
24
25
25
BannerAd _createBannerAd () {
26
+ print ("Create a banner ad" );
26
27
final String bannerId = Platform .isAndroid
27
28
? 'ca-app-pub-3940256099942544/6300978111'
28
29
: 'ca-app-pub-3940256099942544/2934735716' ;
@@ -49,30 +50,29 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
49
50
}
50
51
51
52
BannerAd _getRecycledBannerAd (int bannerPosition) {
52
- // If we already created a banner for this position, just reuse it.
53
- BannerAd ? bannerAd = _banners. firstWhereOrNull ((banner ) => _bannerPositions[banner] == bannerPosition);
54
- if (bannerAd != null ) {
55
- return bannerAd ;
53
+ // If we already created a banner for current position, just reuse it.
54
+ BannerAd ? currentBannerAd = _bannerPositions.entries. firstWhereOrNull ((entry ) => entry.value == bannerPosition)? .key ;
55
+ if (currentBannerAd != null ) {
56
+ return currentBannerAd ;
56
57
}
57
58
58
- // If the cache is not full, create a new banner
59
59
if (_banners.length < _cacheSize) {
60
+ // If the cache is not full, create a new banner
60
61
BannerAd bannerAd = _createBannerAd ();
61
62
_banners.add (bannerAd);
62
63
_bannerPositions[bannerAd] = bannerPosition;
63
64
return bannerAd;
64
- }
65
-
66
- // Now the cache is full, we should recycle the banner (if possible).
67
- BannerAd banner = _banners[bannerPosition % _cacheSize];
68
- if (banner.isMounted) {
69
- // Create a new banner if it's not possible to recycle the banner
70
- // e.g. show 15 banners on screen, but _cacheSize is only 10.
71
- return _createBannerAd ();
72
65
} else {
73
- // Found a recyclable banner, mark it as being used by current banner position.
74
- _bannerPositions[banner] = bannerPosition;
75
- return banner;
66
+ // If cache is full, we should recycle the banner (if possible).
67
+ BannerAd bannerAd = _banners[bannerPosition % _cacheSize];
68
+ if (bannerAd.isMounted) {
69
+ // Create a new banner if it's not possible to recycle the banner
70
+ // e.g. show 15 banners on screen, but _cacheSize is only 10.
71
+ // This should be a corner case indicating _cacheSize should be increased.
72
+ bannerAd = _createBannerAd ();
73
+ }
74
+ _bannerPositions[bannerAd] = bannerPosition;
75
+ return bannerAd;
76
76
}
77
77
}
78
78
0 commit comments