Calvin
Calvin

Reputation: 331

Flutter with multiple Native Ad using Admob

I am using google_mobile_ads for my Flutter app.

Based on the reference provided by Google Flutter, how do I have multiple Native Ads in a ListView (https://codelabs.developers.google.com/codelabs/admob-inline-ads-in-flutter#1).

The example provided only allows 1 Native Ads to be showed in the ListView. My intention is to show different Ads for different entries in the same ListView.

Upvotes: 2

Views: 1397

Answers (1)

Ratnadeep
Ratnadeep

Reputation: 1233

I found it easier to do with flutter_native_admob package rather

You should use ListView.seprated. Try out the below code.

                   ListView.separated(
                        shrinkWrap: true,
                        physics: const AlwaysScrollableScrollPhysics(),
                        itemCount: snapshot.data.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Center(
                            child: Text("List Item")
                          );
                        },
                        separatorBuilder: (BuildContext context, index) {
                          return Container(
                            child: NativeAdmob(
                              adUnitID: ***your ad unit ID***,
                              controller: _nativeAdController,
                              type: NativeAdmobType.full,
                              loading: Center(
                                child: CircularProgressIndicator(),
                              ),
                              error: Center(
                                child: Text(
                                  "Ad failed to load"
                                ),
                              ),
                            ),
                            height: MediaQuery.of(context).size.height*(30/100),
                            padding: EdgeInsets.all(8),
                          );
                        },
                      );

This way you will get native ads after every list items.

Upvotes: 3

Related Questions