4

I am using flutter_native_admob to display ads in my flutter app, link

It is clearly written that to prevent the ads from loading again and again in the list view just save the controller and use it while displaying the ads and this will prevent ads to reload in the list view.

I am doing exactly that but it is doing nothing to solve the problem.

    class ActivityPage extends StatefulWidget {
  static const routeName = "/activityPage";

  @override
  _ActivityPageState createState() => _ActivityPageState();
}

class _ActivityPageState extends State<ActivityPage> {
  final GlobalKey<SummaryState> _summaryKey = GlobalKey<SummaryState>();

  final controller = NativeAdmobController();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    PaymentProvider paymentProvider = Provider.of<PaymentProvider>(context);
    return ChangeNotifierProvider(
      create: (context) => MonthDayDataprovider(
        calendarIndex: (DateTime.now().year * 12 + DateTime.now().month),
        day: DateTime.now().day,
      ),
      child: Scaffold(
        appBar: AppBar(
          brightness: Brightness.light,
          backgroundColor: Color(0xFFFAFAFA),
          title: Text(
            "Activity",
            style: Theme.of(context).textTheme.headline6.copyWith(
                  color: Color(0xFFCE2234),
                ),
          ),
        ),
        backgroundColor: Color(0xFFFAFAFA),
        body: ListView(
          children: <Widget>[
            CalorieStatsCard(),
            BMICard(),
            (paymentProvider.subscription == true ||
                    paymentProvider.removeAds == true)
                ? null
                : NativeBannerAds(
                    controller: this.controller,
                  ),
            Calender(),
            Summary(
              key: this._summaryKey,
            ),
            (paymentProvider.subscription == true ||
                    paymentProvider.removeAds == true)
                ? null
                : NativeFullAds(
                    controller: this.controller,
                  )
          ].where((Object o) => o != null).toList(),
        ),
      ),
    );
  }
}

class NativeBannerAds extends StatefulWidget {
  final NativeAdmobController controller;

  const NativeBannerAds({
    Key key,
    @required this.controller,
  }) : super(key: key);

  @override
  _NativeBannerAdsState createState() => _NativeBannerAdsState();
}

class _NativeBannerAdsState extends State<NativeBannerAds> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      height: 85,
      padding: EdgeInsets.all(12),
      child: NativeAdmob(
        // Your ad unit id
        adUnitID: 'ca-app-pub-xxxxxxxxxxxx/xxxxxxxxxxxxx',
        controller: this.widget.controller,
        error: Center(child: Text("Failed to load the ad")),
        type: NativeAdmobType.banner,
        options: NativeAdmobOptions(
          showMediaContent: true,
          headlineTextStyle: NativeTextStyle(
            backgroundColor: Colors.red,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}

class NativeFullAds extends StatefulWidget {
  final NativeAdmobController controller;

  const NativeFullAds({
    Key key,
    @required this.controller,
  }) : super(key: key);
  @override
  _NativeFullAdsState createState() => _NativeFullAdsState();
}

class _NativeFullAdsState extends State<NativeFullAds> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      height: 375,
      padding: EdgeInsets.all(8),
      child: NativeAdmob(
        // Your ad unit id
        adUnitID: 'ca-app-pub-xxxxxxxxxxxxx/xxxxxxxxxxxx',
        controller: this.widget.controller,
        error: Material(
          child: Center(
            child: Text("Failed to load the ad"),
          ),
        ),
        type: NativeAdmobType.full,
        options: NativeAdmobOptions(
          showMediaContent: true,
          headlineTextStyle: NativeTextStyle(
            backgroundColor: Colors.red,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}

here I am saving the controller to the parent widget and passing it to the child widget but it is not solving the problem, ads still keep loading when scrolled.

How to solve this problem?

enter image description here

rahul Kushwaha
  • 2,395
  • 7
  • 32
  • 64

0 Answers0