1

In my flutter application I am trying to implement google_mobile_ads in the pubspec.yaml I have added the following: google_mobile_ads: ^1.0.1

I have created a banner_ad_widget.dart with the following:


class BannerAdWidget extends StatefulWidget {
  const BannerAdWidget({Key? key}) : super(key: key);

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

class _BannerAdWidgetState extends State<BannerAdWidget> with AdWithViewListener {
  late BannerAd _bannerAd;

  @override
  void initState() {
    super.initState();
    _bannerAd = BannerAd(
      adUnitId: BannerAd.testAdUnitId,
      size: AdSize.banner,
      listener: MyAdListener(),
      request: const AdRequest(),
    )..load();
  }

  @override
  void dispose() {
    _bannerAd.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: _bannerAd.size.height.toDouble(),
      width: _bannerAd.size.width.toDouble(),
      child: AdWidget(ad: _bannerAd),
    );
  }

  @override
  void onAdViewLoaded(Ad ad) {
    setState(() {});
  }
..................................

but I keep getting the following errors:

error: The class 'AdWithViewListener' can't be used as a mixin because it declares a constructor. (mixin_class_declares_constructor at [project] lib\utils\banner_ad_widget.dart:11)
error: The argument type 'MyAdListener' can't be assigned to the parameter type 'BannerAdListener'. (argument_type_not_assignable at [project] lib\utils\banner_ad_widget.dart:20)
error: Classes can only extend other classes. (extends_non_class at [project] lib\utils\banner_ad_widget.dart:66)

My question how to fix this error so that it can work properly without these errors

A_K
  • 731
  • 3
  • 15
  • 40

1 Answers1

1

Why do you want to add AdWithViewListener? You are not even using it anywhere.

You can remove with AdWithViewListener and your code will work fine.

What you are missing is dispose.

  @override
  void dispose() {
    _bannerAd.dispose();
    super.dispose();
  }
Rahul
  • 3,529
  • 1
  • 5
  • 19