3

I've migrated to the new Google Mobile Ads package in Flutter. My interstitial works perfectly but my Native ad show a temporary ad over the majority of my screen before loading into my ListView Container awaiting it with a FutureBuilder. If I keep scrolling it eventually displays as desired. I'm not sure if this is just an issue with the simulator and a larger issue with my code. Here is an example of the issue... Also, I'm not sure if it's my xib file, but I'm using the 1 from the googleads-moble-flutter/NativeAds example HERE

Native Ad Incorrectly Displayed

Here is an example of my code....

Future futureAd;

 @override
  void initState() {
    super.initState();
    futureAd = adBuilder();
  }

 Future adBuilder() async {
    // TODO: Create a NativeAd instance
    final myAd = NativeAd(
      adUnitId: NativeAd.testAdId,
      factoryId: 'listTile',
      request: AdRequest(keywords: ['kpop', 'music', 'youth']),
      listener: AdListener(
        onAdLoaded: (_) {
          setState(() {
            _isAdLoaded = true;
          });
        },
        onAdFailedToLoad: (ad, error) {
          // Releases an ad resource when it fails to load
          ad.dispose();
          setState(() {
            _isAdLoaded = false;
          });

          print(
              'NATIVE AD ERROR ---- Ad load failed (code=${error.code} message=${error.message})');
        },
      ),
    );
    _ad = myAd;
    _ad.load();
    print(_isAdLoaded);
  }

FutureBuilder(
                                future: futureAd,
                                // ignore: missing_return
                                builder: (context, snapshot) {
                                  switch (snapshot.connectionState) {
                                    case (ConnectionState.none):
                                      return Container();
                                    case (ConnectionState.done):
                                      return Container(
                                        height: 300,
                                        width:
                                            MediaQuery.of(context).size.width,
                                        decoration: BoxDecoration(
                                          color: Colors.amberAccent,
                                          borderRadius:
                                              BorderRadius.circular(20),
                                          boxShadow: [
                                            BoxShadow(
                                              color: Colors.grey,
                                              blurRadius: 15,
                                              offset: Offset(
                                                  4, 8), // Shadow position
                                            ),
                                          ],
                                        ),
                                        child: (_isAdLoaded = true) ? AdWidget(ad: _ad) :
                                        Image.asset('assets/loading.gif'),
                                      );
                                    case (ConnectionState.active):
                                      return Container(
                                        height: 300,
                                        width: MediaQuery.of(context).size.width,
                                        child: Image.asset('assets/loading.gif')
                                      );
                                    case (ConnectionState.waiting):
                                      return Container(
                                          height: 300,
                                          width: MediaQuery.of(context).size.width,
                                          child: Image.asset('assets/loading.gif')
                                      );
                                    default:
                                      return Container(
                                        height: 300,
                                        width: MediaQuery.of(context).size.width,
                                        child: Image.asset('logo500.jpeg')
                                      );
                                  }
                                },
                              ),
Charles Jr
  • 8,333
  • 15
  • 53
  • 74

0 Answers0