Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not hide ad immediately after displaying ad on flutter #21474

Closed
ganessaa opened this issue Sep 6, 2018 · 16 comments
Closed

Could not hide ad immediately after displaying ad on flutter #21474

ganessaa opened this issue Sep 6, 2018 · 16 comments
Labels
p: firebase_admob Plugin to show Firebase AdMob ads p: firebase Firebase plugins P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels.

Comments

@ganessaa
Copy link

ganessaa commented Sep 6, 2018

We have created the following class to display AdMob on flutter.

    import 'package:firebase_admob/firebase_admob.dart';
    import 'package:xxxxx/models/const.dart';
    
    class Ads {
      static BannerAd _bannerAd;
    
      static init() {
        FirebaseAdMob.instance.initialize(appId: Const.adAppId);
      }
    
      static BannerAd createBannerAd() {
        return new BannerAd(
          adUnitId: Const.adUnitId,
          size: AdSize.banner,
          targetingInfo: targetingInfo,
          listener: (MobileAdEvent event) {
            print("@@@ $event @@@");
          },
        );
      }
    
      static void showBanner() {
        if (_bannerAd == null) {
          _bannerAd = createBannerAd();
        }
    
        print('@@@ load @@@');
        _bannerAd.load().then((load) {
          print('@@@ show @@@');
          _bannerAd.show(anchorType: AnchorType.bottom);
        });
      }
    
      static void hideBanner() {
        print('@@@ dispose @@@');
        _bannerAd?.dispose();
        print('@@@ null @@@');
        _bannerAd = null;
      }
    
      static final MobileAdTargetingInfo targetingInfo = new MobileAdTargetingInfo(
        testDevices: Const.testDevices,
        keywords: Const.keywords,
        birthday: new DateTime.now(),
        gender: MobileAdGender.female,
      );
    }

The following values are correctly defined in the Const class.

adAppId, adUnitId, testDevices, keywords

init() is called immediately after startup.
showBanner() will be called when showing the ad.
hideBanner() will be called when hiding the ad.

These will work as expected in most cases.

But after _bannerAd.show() is called, the ad will not be hidden if hideBanner() is called before the listener receives MobileAdEvent.loaded.

[OK]
_bannerAd.show() -> receive MobileAdEvent.loaded -> hideBanner()
flutter: @@@ load @@@
flutter: @@@ show @@@
flutter: @@@ MobileAdEvent.loaded @@@
flutter: @@@ dispose @@@
flutter: @@@ null @@@

[NG]
_bannerAd.show() -> hideBanner() -> (receive MobileAdEvent.loaded)
flutter: @@@ load @@@
flutter: @@@ show @@@
flutter: @@@ dispose @@@
flutter: @@@ null @@@

This trouble occurred in ASUS Z017DA. iPad mini, iPhone 6 Plus and iPhone 4S were hidden normally at any timing. We can not test all models, but it may be a problem only for Android.

How can we avoid this if we do?

$ flutter doctor -v
[✓] Flutter (Channel dev, v0.7.5, on Mac OS X 10.13.6 17G65, locale ja)
    • Flutter version 0.7.5 at /Applications/flutter
    • Framework revision eab5cd9853 (6 days ago), 2018-08-30 14:47:04 -0700
    • Engine revision dc7b5eb89d
    • Dart version 2.1.0-dev.3.0.flutter-760a9690c2

[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at /Users/xxxxx/src/android-sdks
    • Android NDK at /Users/xxxxx/src/android-sdks/ndk-bundle
    • Platform android-27, build-tools 27.0.3
    • ANDROID_HOME = /Users/xxxxx/src/android-sdks
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.4.1, Build version 9F2000
    • ios-deploy 1.9.2
    • CocoaPods version 1.5.3

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 27.1.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] VS Code (version 1.25.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.18.0

[✓] Connected devices (2 available)
    • ASUS Z017DA   • XXXXXXXXXXXXXXX                          • android-arm64 • Android 8.0.0 (API 26)
    • iPhone 6 Plus • xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx • ios           • iOS 11.4.1

• No issues found!
@zoechi zoechi added plugin p: firebase Firebase plugins labels Sep 6, 2018
@zoechi zoechi added the p: firebase_admob Plugin to show Firebase AdMob ads label Jan 4, 2019
@AnandGupta7866
Copy link

AnandGupta7866 commented Jan 28, 2019

How to hide ads if we navigate to another page before the ad is loaded????? @zoechi @ganessaa @MistryHiral

@isaachjk
Copy link

bump

@zoechi zoechi added this to the Goals milestone Feb 28, 2019
@jsplashindia
Copy link

Is there any solution to this problem yet?

@d-apps
Copy link

d-apps commented Jul 5, 2019

Same, I can't hide banner from the screen.

@mestartlearncode
Copy link

any idea about how to hide / remove banner immediately ?

@Piotr-Retman
Copy link

I was fighting with this problem same like you but the solution for this to hide Ad should be like this:

  1. Consider creating methods which will
    a) provide the Banner for your app
    b) dispose.

  2. Add ad to your view ininitialState()not build() method (So It should be more like StatefulWidget than StatelessWidget where you use Ads)

  3. Override dispose() method for your component view where you will call the dispose method on your Ad

  4. Done - when you 'travel' from one Route to another it will show and hide the Ad

@izinin
Copy link

izinin commented Sep 25, 2019

The behaviour of ad plugin is different in release and debug mode. i fixed it with delay 0.5 sec

// in the app singleton  class
  BannerAd _myBanner;
  bool _bannerHidden;

void showAdBanner({VoidCallback onLoaded, VoidCallback onError}) {
    if(_myBanner != null) {
      return;
    }
    _bannerHidden = false;
    _myBanner = BannerAd(
....
        listener: (MobileAdEvent event) async {
          if (event == MobileAdEvent.loaded && onLoaded != null) {
            if(_bannerHidden){
               hideAdBanner();
            }else{
               onLoaded();
            }
          } else if (event == MobileAdEvent.failedToLoad && onError != null) {
..
          }
        });
    _myBanner..load()..show(anchorType: AnchorType.bottom);
  }

  void hideAdBanner() {
    Future.delayed(const Duration(milliseconds: 500), () {
      _bannerHidden = true;
      _myBanner?.dispose();
      _myBanner = null;
    });
  }

@kroikie
Copy link

kroikie commented Oct 13, 2019

@ganessaa

This issue has been moved to firebase/flutterfire#669. Any further collaboration will be done there.

@kroikie
Copy link

kroikie commented Oct 14, 2019

Since firebase_admob issues require work on the Flutter side as well as the plugin side these issues will remain open here as well until resolved.

@iTikoz
Copy link

iTikoz commented Apr 27, 2020

workaround could be: using "this.mounted" property of state class before showing the add

  _bannerAd = createBannerAd();
    _bannerAd
  ..load().then((loaded) {
    if (loaded && this.mounted) {
      _bannerAd..show();
    }
  });

That helped me dispose the ad when navigating away from a context

@jacaTM
Copy link

jacaTM commented May 28, 2020

I managed to solve it by creating a boolean that sets to true when I call my function hideBanner(), and checking if it's true when the banner event is loaded. Look at my code:


BannerAd _createBanner(){
    return BannerAd(
      adUnitId: BannerAd.testAdUnitId,
      size: AdSize.banner,
      targetingInfo: getTargetInfo(),
      listener: (MobileAdEvent event) {
        if (event == MobileAdEvent.loaded)
          if(disposed)_bannerAd.dispose();
          else _bannerAd.show(
              anchorOffset: kBottomNavigationBarHeight+15.0,
              anchorType: AnchorType.bottom
          );
      }
    );
  }

  void displayBanner() async {
    disposed = false;
    if(_bannerAd == null) _bannerAd = _createBanner();
    _bannerAd.load();
  }

  void hideBanner() async {
    await _bannerAd?.dispose();
    disposed = true;
    _bannerAd = null;
  }

@kf6gpe kf6gpe added the P2 Important issues not at the top of the work list label May 29, 2020
@Hixie Hixie removed this from the None. milestone Aug 17, 2020
@BoHellgren
Copy link

I have spent many hours debugging this dispose problem. And found a perfect solution: Don't use the firebase_admob plugin! Use the admob_flutter plugin instead. Very easy to use and works like a dream.

@srsudar
Copy link

srsudar commented Feb 4, 2021

jacaTM's answer hides the ad for me correctly (finally!), but I'm occasionally getting this error:

'package:firebase_admob/firebase_admob.dart': Failed assertion: line 249 pos 12: '_allAds[id] != null': is not true.

Has anyone figured out how to resolve this? Not sure where I'm going wrong.

@FonzTech
Copy link

FonzTech commented Mar 23, 2021

@srsudar I fixed this error by adding a line like this in the listener function for my BannerAd:
print("FixForHashcode " + DateTime.now().millisecondsSinceEpoch.toString());
so the id variable will change "forcefully", letting the assert become true.
But remember that you have to reinstantiate your ad unit object every time after dispose !
I'm using version 0.11.0+1.
Let us know.

@stuartmorgan
Copy link
Contributor

Closing, as firebase_admob is now deprecated.

@github-actions
Copy link

github-actions bot commented Aug 1, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: firebase_admob Plugin to show Firebase AdMob ads p: firebase Firebase plugins P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels.
Projects
None yet
Development

No branches or pull requests