#StackBounty: #flutter #dart #admob #observable BannerAd.dispose Not Working Flutter Admob

Bounty: 50

I’m currently trying to display an admob banner in my app, However there are two pages where I would not like the banner displayed (For now i’m testing with just the settings route), And calling dispose on the banner does not hide it.

What I tried:

  1. Creating the banner for each widget and calling the dispose method inside the override of the widget’s onDispose method
  2. Creating an Ad manager class that created the banner and saved a reference to it, and invoking a function from said class that calls dispose on the banner inside the init of the page I don’t want the banner shown on.
  3. My current solution: an observer class that shows the banner on push, and if the name of the route matches what I’m looking for hides the banner.

So far none of these approaches have been successful 🙁 What am I doing wrong? Am I missing something?

Observer class:

class AdmobObserver extends RouteObserver<PageRoute<dynamic>> {

  static ValueNotifier<bool> isBannerAdShowing = ValueNotifier<bool>(false);

  BannerAd _myBanner = BannerAd(
    adUnitId: AdManager.bannerAdUnitId,
    size: AdSize.banner,
    listener: (MobileAdEvent event) {
      if (event == MobileAdEvent.loaded) {
        isBannerAdShowing.value = true;
      }
      else if (event == MobileAdEvent.failedToLoad) {
        isBannerAdShowing.value = false;
      }
    },
  );

  @override
  void didPush(Route route, Route previousRoute) {
    super.didPush(route, previousRoute);
    if (route.settings.name != '/settings') {
      _showBannerAd();
    } else {
      _myBanner.dispose();
      isBannerAdShowing.value = false;
    }

  }

  @override
  void didPop(Route route, Route previousRoute) {
    super.didPop(route, previousRoute);
    _showBannerAd();
  }

  void _showBannerAd() {
    _myBanner
      ..load()
      ..show();
  }
}

Main App:

class MyApp extends StatelessWidget {

  static AdmobObserver admobObserver = AdmobObserver();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'Montserrat',
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      navigatorObservers: <NavigatorObserver>[admobObserver],
      initialRoute: '/',
      onGenerateRoute: RouteGenerator.generateRoute,
    );
  }
}

EDIT:
it seems as if the Routes passed to the didPush method in my observer are null, So here are the logs and my route generator:

log(route):

 MaterialPageRoute<dynamic>(RouteSettings("null", null), animation: AnimationController#bc4fa(▶ 0.000; for MaterialPageRoute<dynamic>(null)))

RouteGenerator:

class RouteGenerator{
  static Route<dynamic> generateRoute(RouteSettings settings){
    final args = settings.arguments;

    switch(settings.name){
      case '/':
        Map<String,dynamic> argus = args;
        if(argus == null || argus['message'] == null)
          return MaterialPageRoute(builder: (_) => MyHomePage());
        return MaterialPageRoute(builder: (_) => MyHomePage(message:argus['message']));
      case '/settings':
        if(args is bool){
          return MaterialPageRoute(builder: (_) => Settings(perms: args,));
        }
        return _errorRoute();
  }
  static Route<dynamic> _errorRoute({String message = "Unexpected Arguments"}){
    return MaterialPageRoute(builder: (_) => ErrorPage(message));
  }
}

Example of pushing the next page:

 final u = await Auth().getCurrentUser();
 final arg = u != null;
 Navigator.pushNamed(context, '/settings', arguments: arg);


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.