lazy_ads 0.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

lazy_ads #

A plugin for Flutter that supports loading and displaying banner, interstitial (full-screen), and rewarded video ads using the Firebase AdMob API.

Note: This plugin is in beta, and may still have a few issues and missing APIs. Feedback and Pull Requests are welcome.

AndroidManifest changes #

AdMob 17 requires the App ID to be included in the AndroidManifest.xml. Failure to do so will result in a crash on launch of your app. The line should look like:

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="[ADMOB_APP_ID]"/>

where [ADMOB_APP_ID] is your App ID. You must pass the same value when you initialize the plugin in your Dart code.

See https://goo.gl/fQ2neu for more information about configuring AndroidManifest.xml and setting up your App ID.

Info.plist changes #

Admob 7.42.0 requires the App ID to be included in Info.plist. Failure to do so will result in a crash on launch of your app. The lines should look like:

<key>GADApplicationIdentifier</key>
<string>[ADMOB_APP_ID]</string>

where [ADMOB_APP_ID] is your App ID. You must pass the same value when you initialize the plugin in your Dart code.

See https://developers.google.com/admob/ios/quick-start#update_your_infoplist for more information about configuring Info.plist and setting up your App ID.

Initializing the plugin #

The AdMob plugin must be initialized with an AdMob App ID.

FirebaseAdMob.instance.initialize(appId: appId);

Android #

Starting in version 17.0.0, if you are an AdMob publisher you are now required to add your AdMob app ID in your AndroidManifest.xml file. Once you find your AdMob app ID in the AdMob UI, add it to your manifest adding the following tag:

<manifest>
    <application>
        <!-- TODO: Replace with your real AdMob app ID -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-################~##########"/>
    </application>
</manifest>

Failure to add this tag will result in the app crashing at app launch with a message starting with "The Google Mobile Ads SDK was initialized incorrectly."

On Android, this value must be the same as the App ID value set in your AndroidManifest.xml.

iOS #

Starting in version 7.42.0, you are required to add your AdMob app ID in your Info.plist file under the Runner directory. You can add it using Xcode or edit the file manually:

<dict>
	<key>GADApplicationIdentifier</key>
	<string>ca-app-pub-################~##########</string>
</dict>

Failure to add this tag will result in the app crashing at app launch with a message including "GADVerifyApplicationID."

Using banners and interstitials #

Banner and interstitial ads can be configured with target information. And in the example below, the ads are given test ad unit IDs for a quick start.

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
  keywords: <String>['flutterio', 'beautiful apps'],
  contentUrl: 'https://flutter.io',
  birthday: DateTime.now(),
  childDirected: false,
  designedForFamilies: false,
  gender: MobileAdGender.male, // or MobileAdGender.female, MobileAdGender.unknown
  testDevices: <String>[], // Android emulators are considered test devices
);

BannerAd myBanner = BannerAd(
  // Replace the testAdUnitId with an ad unit id from the AdMob dash.
  // https://developers.google.com/admob/android/test-ads
  // https://developers.google.com/admob/ios/test-ads
  adUnitId: BannerAd.testAdUnitId,
  size: AdSize.smartBanner,
  targetingInfo: targetingInfo,
  listener: (MobileAdEvent event) {
    print("BannerAd event is $event");
  },
);

InterstitialAd myInterstitial = InterstitialAd(
  // Replace the testAdUnitId with an ad unit id from the AdMob dash.
  // https://developers.google.com/admob/android/test-ads
  // https://developers.google.com/admob/ios/test-ads
  adUnitId: InterstitialAd.testAdUnitId,
  targetingInfo: targetingInfo,
  listener: (MobileAdEvent event) {
    print("InterstitialAd event is $event");
  },
);

Ads must be loaded before they're shown.

myBanner
  // typically this happens well before the ad is shown
  ..load()
  ..show(
    // Positions the banner ad 60 pixels from the bottom of the screen
    anchorOffset: 60.0,
    // Banner Position
    anchorType: AnchorType.bottom,
  );
myInterstitial
  ..load()
  ..show(
    anchorType: AnchorType.bottom,
    anchorOffset: 0.0,
  );

BannerAd and InterstitialAd objects can be disposed to free up plugin resources. Disposing a banner ad that's been shown removes it from the screen. Interstitial ads, however, can't be programmatically removed from view.

Banner and interstitial ads can be created with a MobileAdEvent listener. The listener can be used to detect when the ad has actually finished loading (or failed to load at all).

Using rewarded video ads #

Unlike banners and interstitials, rewarded video ads are loaded one at a time via a singleton object, RewardedVideoAd.instance. Its load method takes an AdMob ad unit ID and an instance of MobileAdTargetingInfo:

RewardedVideoAd.instance.load(myAdMobAdUnitId, targetingInfo);

To listen for events in the rewarded video ad lifecycle, apps can define a function matching the RewardedVideoAdListener typedef, and assign it to the listener instance variable in RewardedVideoAd. If set, the listener function will be invoked whenever one of the events in the RewardedVideAdEvent enum occurs. After a rewarded video ad loads, for example, the RewardedVideoAdEvent.loaded is sent. Any time after that, apps can show the ad by calling show:

RewardedVideoAd.instance.show();

When the AdMob SDK decides it's time to grant an in-app reward, it does so via the RewardedVideoAdEvent.rewarded event:

RewardedVideoAd.instance.listener =
    (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
  if (event == RewardedVideoAdEvent.rewarded) {
    setState(() {
      // Here, apps should update state to reflect the reward.
      _goldCoins += rewardAmount;
    });
  }
};

Because RewardedVideoAd is a singleton object, it does not offer a dispose method.

Limitations #

This is just an initial version of the plugin. There are still some limitations:

  • Banner ads have limited positioning functionality. They can be positioned at the top or the bottom of the screen and at a logical pixel offset from the edge.
  • Banner ads cannot be animated into view.
  • It's not possible to specify a banner ad's size.
  • There's no support for native ads.
  • The existing tests are fairly rudimentary.
  • There is no API doc.
  • The example should demonstrate how to show gate a route push with an interstitial ad

For Flutter plugins for other Firebase products, see FlutterFire.md.

0.0.2 #

  • updated readme & changelog files.

0.0.1 #

  • Based on firebase_admob v0.9.0

example/README.md

firebase_admob_example #

Demonstrates how to use the firebase_admob plugin.

Getting Started #

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  lazy_ads: ^0.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:lazy_ads/lazy_ads.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
49
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
86
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

The package description is too short. (-4 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
meta ^1.0.4 1.1.7
platform ^2.0.0 2.2.1
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
firebase_core ^0.4.0
flutter_test
mockito ^3.0.0