Like for previous FlutterFire plugins, we need to add the dependency for AdMob to our pubspec.yaml, as follows:
dependencies:
firebase_admob: ^0.8.0+4 # AdMob
After getting dependencies with flutter packages get, we are ready to use Firebase AdMob in our project.
The FirebaseAdMob class is our starting point to add banners to the application. Unlike previously seen Firebase plugins that get all the information needed to run from the google-services.json (Android) and GoogleService-info.plist (iOS) files, in this case, we need an additional setting before we can use the plugin effectively.
We need to manually initialize the plugin with our application IDs. This can be done at any point. In our Favors app, for example, we can do this in the main method as shown here:
void main() {
FirebaseAdMob.instance.initialize(
appId: Platform.isAndroid
? 'ca-app-pub-3940256099942544~3347511713' // replace with your Android app id
: 'ca-app-pub-3940256099942544~1458002511', // replace with your iOS app id
);
runApp(MyApp());
}
As you can see, we initialize the plugin by providing our registered app ID (important for release). In the previous example, we are using just test IDs. This is the same value as present in the library's FirebaseAdMob.testAppId property. We can test our banners in the following two ways:
- By using test ads provided by Google. With this, we use a set of mock ads, with no real traffic in our application ads.
- By adding testing devices with our real IDs. This is the preferred option, as it means we have the real look of the ads.