Flutter, Facebook Audience Network ads banner not showing when enter another screen

Viewed 1601

I use this plugin

facebook_audience_network 0.5.0

and I implemented this by this way

class FacebookAd{

static void faceInit() {
 FacebookAudienceNetwork.init(
   testingId: "xxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx",
 );
}

static widget myBanner;

static void showBannerAd() {
  myBanner = FacebookBannerAd(
    placementId: "xxxxxxxxx_xxxxxxxxxxx",
    keepAlive: true,
    bannerSize: BannerSize.STANDARD,
    listener: (result, value) {
      print("Banner Ad: $result -->  $value eee");
      switch (result) {
        case BannerAdResult.ERROR:
          print("Error: $value");
          break;
        case BannerAdResult.LOADED:
          print("Loaded: $value");
          break;
        case BannerAdResult.CLICKED:
          print("Clicked: $value");
          break;
        case BannerAdResult.LOGGING_IMPRESSION:
          print("Logging Impression: $value");
          break;
      }
    },
  );
 }
}

}

and from other classes

 return Scaffold(
   bottomNavigationBar: FacebookAd.myBanner
 )

so in the app, all of screens refer variable "myBanner" to show banner inside "bottomNavigationBar"

but the problem is when the screen is changed using pushNamed("/xxx")

sometimes the screen showing banner and sometimes not ! (mostly not)

how do I solve this problem this?

thank you

1 Answers

Here is how I was able to solve the FAN interstitials for different pages, unlike the AdMob you should manage the Facebook FAN ads centrally in a separate dart class.

class MyFacebookAdsManager {
  static bool isInterstitialAdLoaded = false;
  static String adsInterstitialUnitId = '';

  static void init(String id) {
    logger.i('$prefix init called');
    adsInterstitialUnitId = id;
    FacebookAudienceNetwork.init(
      testingId: '<test-id-if-needed>',
    );
    loadNewFANInterstitialAd();
  }

 static void loadNewFANInterstitialAd() {
    FacebookInterstitialAd.loadInterstitialAd(
      placementId: adsInterstitialUnitId,
      listener: (result, value) {
        logger.i("$prefix >> FAN > Interstitial Ad: $result --> $value");
        switch (result) {
          case InterstitialAdResult.LOADED:
            isInterstitialAdLoaded = true;
            break;

          case InterstitialAdResult.DISMISSED:
            isInterstitialAdLoaded = false;
            loadNewFANInterstitialAd();
            break;
          case InterstitialAdResult.ERROR:
            isInterstitialAdLoaded = false;
            logger.e('error loading facebook ads');
            loadNewFANInterstitialAd();
            break;
          default:
        }
      },
    );
  }

  static void showInterstitialAd() {
    if (isInterstitialAdLoaded)
      FacebookInterstitialAd.showInterstitialAd();
    else
      loadNewFANInterstitialAd();
  }
}

Now in the main screen you will call the init once and only once.

MyFacebookAdsManager.init('test-id')

Finally, wherever you want to display the Ad, use the class created and show the ad.

MyFacebookAdsManager.showInterstitialAd();

In the same fashion, you should create separate classes and manage All banners & reward videos.

Related