Search code examples
flutterdialogadmobinterstitial

How to wait interstitial ad with waiting dialog in flutter


Here is an article which says you shouldn’t surprise users with interstitial ads.

I have code like this to delay an ad before it appears:

void _onLoading() {
showDialog(
    context: context,
    barrierDismissible: false,
    builder: (context) => AlertDialog(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(32.0))),
          contentPadding: EdgeInsets.only(top: 10.0),
          content: Container(
            margin: const EdgeInsets.all(10.0),

              child: new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  new CircularProgressIndicator(),
                  SizedBox(width: 16.0),
                  new Text("Please wait.."),
                ],
              )),
        ));

new Future.delayed(new Duration(seconds: 3), () {
  Navigator.pop(context); //pop dialog
  createInterstitialAd()
    ..load()
    ..show();
});}

But that code just waits for 3 seconds and does not depend on interstitial ad when it appears. In that case, if after 3 second and if the connection is still slow, it will remain silent. How can I handle that until interstitial ad has really appeared. Thanks.


Solution

  • I use this in my code :

    InterstitialAd myInterstitialAd = InterstitialAd(
            adUnitId: <youradUnitId>,
            targetingInfo: MobileAdTargetingInfo(
              childDirected: true,
              nonPersonalizedAds: true,
            ),
            listener: (event) {
              if (event == MobileAdEvent.opened || event == MobileAdEvent.failedToLoad) {
                <go to another page>
              }
            });