简体   繁体   中英

Google mobile Ads implementation slowed down flutter App

I've been implementing a banner ad on my flutter app and I manage to show the banner successfully but my app goes really slow to the point it then crashes and closes. I've been looking quite a lot for a solution to this problem but can't seem to find any although alot of people are experiencing the same. Would really appreciate if you guys could help me.

My code follows:

adhelper.dart

import 'dart:io';
import 'package:google_mobile_ads/google_mobile_ads.dart';

class AdHelper {

  Future<InitializationStatus> initialization;

  AdHelper(this.initialization);

  static String get bannerAdUnitId => Platform.isAndroid
  ? 'ca-app-pub-3940256099942544/6300978111' \\Test ads
  : 'ca-app-pub-3940256099942544/2934735716'; \\Test ads


}

mainpage.dart

class _MainPageState extends State<MainPage>...

  final Completer<BannerAd> bannerCompleter = Completer<BannerAd>();

...

  BannerAd banner;

...

@override
  void didChangeDependencies(){
    super.didChangeDependencies();
    final adHelper = Provider.of<AdHelper>(context);
    adHelper.initialization.then((status){
      setState(() {
        banner ??= BannerAd(
        adUnitId: AdHelper.bannerAdUnitId,
        size: AdSize.banner,
        request: AdRequest(),
          listener: AdListener(
            onAdLoaded: (Ad ad) {
              print('Ad loaded: $BannerAd.');
              bannerCompleter.complete(ad as BannerAd);
              },
            onAdFailedToLoad: (Ad ad, LoadAdError error) {
              print('Ad failed to load: $BannerAd, $error');
              bannerCompleter.completeError(null);
              },
            onAdOpened: (Ad ad) => print('Ad opened: $BannerAd.'),
            onAdClosed: (Ad ad) => print('Ad closed: $BannerAd.'),
            onApplicationExit: (Ad ad) => print('App Exit: $BannerAd.'),
              ),
            );
          Future<void>.delayed(Duration(seconds: 1), () => banner?.load());
      });
    });
  }

@override
  void dispose() {
    super.dispose();
    banner?.dispose();
    banner = null;
  }

@override
  Widget build(BuildContext context) {
    return FutureBuilder<BannerAd>(
      future: bannerCompleter.future,
      builder: (BuildContext context, AsyncSnapshot<BannerAd> snapshot) {
        Widget child;

        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
          case ConnectionState.active:
            child = Container();
            break;
          case ConnectionState.done:
            if (snapshot.hasData) {
              child = AdWidget(ad: banner);
            } else {
              child = Text('Error loading $BannerAd');
            }
        }
...

                      if (banner == null)
                        SizedBox(height: 50,)
                      else
                        Container(
                        width: banner.size.width.toDouble(),
                        height: banner.size.height.toDouble(),
                          child: child,

and my main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final initFuture = MobileAds.instance.initialize();
  final adHelper = AdHelper(initFuture);

....

runApp(
      Provider.value(
          value: adHelper,
        builder: (context, child) => MyApp(),
      ),

This problem occurs only on devices using Android 10 or below, I had the same issue on my device( with android 10). I recommend using this plugin, it solved the problem for me and for a lot of other people.

native_admob_flutter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM