0

Say you have a stateful widget (name it ÌnterstitialAd) which implements a Google AdMob interstitial ad; you placed it in the parent splash screen in the hope it shows the ad up.

Unfortunately, the splash makes some setState() hence triggering the rebuild many times.

As a consequence, ÌnterstitialAd triggers many interstitial ad.

What would be the best approach to prevent this issue?

Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95

1 Answers1

0

Try to trigger the function in initState() with a delay.

Like with Future.delayed(). This will ensure the method is called only once.

 @override
  void initState() {
    getRoutes();
    Future.delayed(const Duration(seconds: 1), () {
      //Trigger some method here, it will execute only once
    });
    super.initState();
  }

If you need to call the method from inside of build method try as below:

    bool isTriggered = false;
    
     @override
      Widget build(BuildContext context) {
        if(!isTriggered){
              //Trigger some method here
            isTriggered = true;
        }
        return Scaffold(
            body: Column(
                children: [
                 
                ],
           ),
        );
    }