Home > Mobile >  MaterialApp widget not updating after calling method in main() on launch
MaterialApp widget not updating after calling method in main() on launch

Time:10-10

I have a AdMob banner widget that is conditionally displayed from a method being called in main(). Since I want it displayed across all screens, I have it placed inside a Container as a child of MaterialApp with main contents widget attached to "home" attribute.

Issue I'm having is that, even after the method is called to load the banner successfully, the banner widget isn't displayed on launch. However, when the view is refreshed, it does.

What changes would I have to make to the code so the banner widget gets displayed at the same time with MaterialApp?

This is the code for loading the widget:

runApp(MaterialApp(
    home: MyHomePage(),
    // debugShowCheckedModeBanner: false,
    builder: (BuildContext context, Widget child) {
      child = EasyLoading.init()(context,child);
      AdLoader().loadBanner();
      return Container(
        color: resultDisabled ? Color(0xff77c2ff) : Color(0xFF55A13B),
        child: SafeArea(
          top: false,
          child: Column(
            children: [
              Expanded(child: child),
              if (AdLoader.anchoredBanner != null)
                AdLoader().bannerWidget(AdLoader.anchoredBanner),
            ],
          ),
        ),
      );
    },

And the method ran on main() to load the banner:

Future<void> loadBanner() async {
    final bannerAd = BannerAd(
      adUnitId: Platform.isAndroid ? realUnitIdAndroid : realUnitIdiOS,
      size: AdSize.fullBanner,
      request: request,
      listener: BannerAdListener(
        // Called when an ad is successfully received.
        onAdLoaded: (Ad ad) {
          print('Ad loaded.');

          anchoredBanner = ad as BannerAd;
        },
        // Called when an ad request failed.
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          // Dispose the ad here to free resources.
          ad.dispose();
          print('Ad failed to load: $error');
        },
        // Called when an ad opens an overlay that covers the screen.
        onAdOpened: (Ad ad) => print('Ad opened.'),
        // Called when an ad removes an overlay that covers the screen.
        onAdClosed: (Ad ad) => print('Ad closed.'),
        // Called when an impression occurs on the ad.
        onAdImpression: (Ad ad) => print('Ad impression.'),
      ),
    );

    bannerAd.load();

  }

And code for the banner widget:

  Widget bannerWidget(BannerAd ad) {
    return Container(
      color: Colors.green,
      width: anchoredBanner.size.width.toDouble(),
      height: anchoredBanner.size.height.toDouble(),
      child: AdWidget(ad: ad),
    );
  }
}

CodePudding user response:

You need to update state at the end in loadBanner function

Future<void> loadBanner() async {

 ....

//update state 
}

CodePudding user response:

Try this:

In main:

 void refresh() {
 setState(() {});
  }
  

In loadBanner

loadBanner(Function() refresh){
 ....
 
refresh();
}

When calling loadBanner in main:

AdLoader().loadBanner(refresh);
  • Related