i am using google_mobile_ads: ^5.2.0 to display adds on my flutter app. I need to request and display 3 different sizes of banner ads from google.

This ad is supposed to be show in a scrollable column so i must set the height of it, the problem is that when the add is loaded i don't have the information about the loaded ad, it could be any of the three. If i don't set the height it is either hidden or there is an error that the height cannot be infinite.

note: in the snippet in only set the temporary size, because i don't know how to get the correct one.

 Future<void> loadAd() async {     _bannerAd = AdManagerBannerAd(         adUnitId: adUnitId,         request: const AdManagerAdRequest(),         sizes: [           const AdSize(width: 300, height: 100),           const AdSize(width: 300, height: 250),           const AdSize(width: 300, height: 600)         ],         listener: AdManagerBannerAdListener(           onAdLoaded: (ad) {             if (!mounted) {               ad.dispose();               return;             }             debugPrint('$ad loaded.');             setState(() {               _isLoaded = true;             });           },           onAdFailedToLoad: (ad, error) {             debugPrint('AdManagerBannerAd failed to load: $error');             ad.dispose();           },         ))       ..load();   }   @override   void dispose() {          _bannerAd?.dispose();     super.dispose();   }   @override   Widget build(BuildContext context) {     return _isLoaded && _bannerAd != null         ? Align(             alignment: Alignment.bottomCenter,             child: SafeArea(               child: SizedBox(                 width: _bannerAd!.sizes[0].width.toDouble(),                 height: _bannerAd!.sizes[0].height.toDouble(),                 child: AdWidget(ad: _bannerAd!),               ),             ),           )         : const SizedBox             .shrink();    } 

please help.

Tag:google-ads-api, android, flutter, google-ad-manager, google-mobile-ads

Add a new comment.