AndriousSolutions / ads

Other
58 stars 11 forks source link

how do use the following #20

Closed redimongo closed 4 years ago

redimongo commented 4 years ago

So I copied the following code and yes I changed my admob appId

however, I cant seem to call a test ad when i import the ad.dart file into the script.

import 'package:flutter/material.dart';
import 'dart:io' show Platform;
import 'package:firebase_admob/firebase_admob.dart';
import 'package:ads/ads.dart';

class AppAds {
  static Ads _ads;

  static final String _appId = Platform.isAndroid
      ? 'ca-app-pub-4999865903647931~XXXXXXXX'
      : 'ca-app-pub-4999865903647931~XXXXXXXXX';

  static final String _bannerUnitId = Platform.isAndroid
      ? 'ca-app-pub-4999865903647931/XXXXXXXX'
      : 'ca-app-pub-4999865903647931/XXXXXXXXX';

  /// Assign a listener.
  static AdEventListener _eventListener = (MobileAdEvent event) {
    if (event == MobileAdEvent.clicked) {
      print("The opened ad is clicked on.");
    }
  };

  static void showBanner(
      {String adUnitId,
        AdSize size,
        List<String> keywords,
        String contentUrl,
        bool childDirected,
        List<String> testDevices,
        bool testing,
        AdEventListener listener,
        State state,
        double anchorOffset,
        AnchorType anchorType}) =>
      _ads?.showBannerAd(
          adUnitId: adUnitId,
          size: size,
          keywords: keywords,
          contentUrl: contentUrl,
          childDirected: childDirected,
          testDevices: testDevices,
          testing: testing,
          listener: listener,
          state: state,
          anchorOffset: anchorOffset,
          anchorType: anchorType);

  static void hideBanner() => _ads?.hideBannerAd();

  /// Call this static function in your State object's initState() function.
  static void init() => _ads ??= Ads(
    _appId,
    bannerUnitId: _bannerUnitId,
    keywords: <String>['ibm', 'computers'],
    contentUrl: 'http://www.ibm.com',
    childDirected: false,
    testDevices: ['SM_A305YN:5554'],
    testing: true,
    listener: _eventListener,
  );

  /// Remember to call this in the State object's dispose() function.
  static void dispose() => _ads?.dispose();
}

I want to show the Banner on my news articles. You can see AppAds.showBannerAd(); but it brings an error Compiler message: lib/model/ads.dart:19:10: Error: Type 'AdEventListener' not found. static AdEventListener _eventListener = (MobileAdEvent event) { ^^^^^^^^^^^^^^^ lib/model/ads.dart:33:9: Error: Type 'AdEventListener' not found. AdEventListener listener, ^^^^^^^^^^^^^^^ lib/screens/newswidget.dart:156:12: Error: Method not found: 'AppAds.showBannerAd'. AppAds.showBannerAd(); ^^^^^^^^^^^^ lib/model/ads.dart:19:10: Error: 'AdEventListener' isn't a type. static AdEventListener _eventListener = (MobileAdEvent event) { ^^^^^^^^^^^^^^^ lib/model/ads.dart:33:9: Error: 'AdEventListener' isn't a type. AdEventListener listener,

import 'package:DRN1/model/ads.dart';
class SecondRoute extends StatelessWidget {
  final article;

    // In the constructor, require a Todo.
  SecondRoute({Key key, @required this.article}) : super(key: key);
  @override
  Widget build(BuildContext context) {

    AppAds.showBannerAd();
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.redAccent,
        title: Text(article['title']),
      ),
      body: Center(
                  child: Container(
                    margin: const EdgeInsets.only(bottom: 40.0),
                      child: new SingleChildScrollView(
                        child: Card(
                          child: new Container(
                          padding: new EdgeInsets.all(10.0),
                                  child: Column(
                                      children: <Widget>[
                                        Image.network(article['image'], fit: BoxFit.fitWidth),
                                        Html(data: article['content']),

                                      ]
                                  ),
                              ),
                        ),
                      ),
                      )

      )
    );

  }
}
Andrious commented 4 years ago

@redimongo Hi there!

Good to see you're making a 'static wrapper' class. It's almost finished but 'AdEventListener' isn't a type. It is not defined.

Change the following line: static AdEventListener _eventListener = (MobileAdEvent event) {

To this line: static MobileAdListener _eventListener = (MobileAdEvent event) {

redimongo commented 4 years ago

Are you sure about that comment above? as AdEventListener listener, is in the static void showBanner(

 /// Assign a listener.
  static AdEventListener _eventListener = (MobileAdEvent event) {
    if (event == MobileAdEvent.clicked) {
      print("The opened ad is clicked on.");
    }
  };

  static void showBanner(
      {String adUnitId,
        AdSize size,
        List<String> keywords,
        String contentUrl,
        bool childDirected,
        List<String> testDevices,
        bool testing,
        AdEventListener listener,
Andrious commented 4 years ago

Yes it is found in the wrapper code you wrote, but you didn't define it. You're error message even says so: "... Error: 'AdEventListener' isn't a type."

What happens when you change the line?

The type, AdEventListener, was found in old versions of the library, but replaced by a MobileAdListener

  Future<bool> showBannerAd({
    String adUnitId,
    MobileAdTargetingInfo targetInfo,
    List<String> keywords,
    String contentUrl,
    bool childDirected,
    List<String> testDevices,
    bool testing,
    MobileAdListener listener,
    AdSize size,
    double anchorOffset,
    double horizontalCenterOffset,
    AnchorType anchorType,
    State state,

Ensure you're using the most 'up-to-date' library package with the following in your pubspec.yaml:

dependencies:
  ads:^1.0.0