AndriousSolutions / ads

Other
58 stars 11 forks source link

Undefined Class AdEventListener #26

Closed ricristian closed 4 years ago

ricristian commented 4 years ago

Hello I've tried to instantiate Ads in separate AppAds.dart file but I receive in error on AdEventListener

Also is really hard for a beginer to implement all ads function in separate file and add as a single tone.. A fully example of this would be really appreciated.

Many thanks

image

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

class AppAds {
  static Ads _ads;

  static final String _appId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544~3347511713'
      : 'ca-app-pub-3940256099942544~1458002511';

  static final String _bannerUnitId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/6300978111'
      : 'ca-app-pub-3940256099942544/2934735716';

  final String screenUnitId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/1033173712'
      : 'ca-app-pub-3940256099942544/4411468910';
  final String videoUnitId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/5224354917'
      : 'ca-app-pub-3940256099942544/1712485313';

  /// 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: ['Samsung_Galaxy_SII_API_26:5554'],
        testing: false,
        listener: _eventListener,
      );

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

The type, AdEventListener, is found in old versions but replaced by a MobileAdListener I apologize as it would seem the example code was not updated to reflect this. The README.md file will be updated and corrected as soon as possible.

Replace every instance of AdEventListener with MobileAdListener, and should find that error is fixed.

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

dependencies:
  ads:^1.0.0

"Also is really hard for a beginner to implement all ads function in separate file and add as a single tone.. A fully example of this would be really appreciated."

Please, example your second question further.

Greg

ricristian commented 4 years ago
'hideBannerAd' is deprecated and shouldn't be used.
Try replacing the use of the deprecated member with the replacement.

image

ads: ^1.7.0

Regarding my 2nd question is that I'm a begginer and I would like to see an example of separate main.dart logic into AppAds.dart. In this moment in AppAds.dart file is only show banner covered but there are 2 more examples ( reward video & Interstitial )

I belive that something like ( Ok you have all banners and listeners in AppAds.dart ) and then I can call it from another library / widget .. in order to avoid 'ads is already instantiated' How can I do it for each banner ?

Also this is a offtopic but I saw that after I've loaded banners first time ram consumption from my phone ( Samsung S 10 ) increase to much ( from ~110MB to 200 ) si it's about 90MB of ram that those ads eats ...

Basically in readme in the section were you wrote about "Keep It Static" with example:

class AppAds {
  static Ads _ads;

  static final String _appId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544~3347511713'
      : 'ca-app-pub-3940256099942544~1458002511';

  static final String _bannerUnitId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/6300978111'
      : 'ca-app-pub-3940256099942544/2934735716';

  /// Assign a listener.
  static MobileAdListener _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,
          MobileAdListener 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?.closeBannerAd();

  /// 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: ['Samsung_Galaxy_SII_API_26:5554'],
        testing: false,
        listener: _eventListener,
      );

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

In would love to see that example fully completed with all cases because frankly Below only the showBannerAd() and dispose() functions are implemented, but you'll get the idea and should able to implement any and all the functions you require: I dont' have any idea how to implement for video for example

Andrious commented 4 years ago

Is this still an issue?