我正在尝试将admob集成到颤振中。它返回空值?

时间:2020-07-15 14:03:01

标签: flutter dart

我正在尝试使用admob监控我的应用程序,但是很难在应用程序中实现它,现在一切都已设置,但是会出现以下错误,“方法'load'在null上调用。 接收者:null 尝试调用:load()“ 有人可以帮忙吗?

import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:animated_dialog/animated_dialog.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:firebase_admob/firebase_admob.dart';

class Car extends StatefulWidget {
  @override
  _CarState createState() => _CarState();
}

class _CarState extends State<Car> {
  static String adId = 'ca-app-pub-xxxxx92680942917/5470xxxxxx';
  String appId = 'ca-app-pub-xxxxxx2680942917~612810xxxx';
  MobileAdTargetingInfo targetingInfo;
  BannerAd myBanner;
  @override
  void initState() {
    super.initState();
    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(
      adUnitId: adId,
      size: AdSize.fullBanner,
      targetingInfo: targetingInfo,
      listener: (MobileAdEvent event) {
        print("BannerAd event is $event");
      },
    );
  }

  showBanner() {
    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: 0.0,
        // Positions the banner ad 10 pixels from the center of the screen to the right
        horizontalCenterOffset: 0.0,
        // Banner Position
        // anchorType: AnchorType.bottom,
      );
  }

  @override
  Widget build(
    BuildContext context,
  ) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.teal[50],
        body: StreamBuilder(
          stream: Firestore.instance.collection('car').snapshots(),
          builder: (context, snapshot) {
            if (snapshot.data == null)
              return Center(
                child: CircularProgressIndicator(
                  backgroundColor: Colors.red,
                  valueColor: new AlwaysStoppedAnimation<Color>(Colors.teal),
                ),
              );

            return ListView.builder(
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) => SingleChildScrollView(
                child: Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Container(
                      child: Card(
                          //shadowColor: Colors.yellow,
                          child: Column(
                        children: <Widget>[
                          //AD
                          showBanner(),
                          // Name Container
                          Container(
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.teal[300],
                                borderRadius: BorderRadius.only(
                                    topRight: Radius.circular(15),
                                    topLeft: Radius.circular(15))),
                            width: 400,
                            height: 50,
                            child: Text(
                              snapshot.data.documents[index]['item Name'],
                              style: TextStyle(
                                fontFamily: 'Baloo',
                                fontSize: 25,
                                color: Colors.white,
                              ),
                            ),
                          ),
                          //item Image///////////////////////////////////////////////////////////////////////////////
                          

我删除了其余的代码,因为它很长,可以在这里输入。

1 个答案:

答案 0 :(得分:0)

之所以这样,是因为您没有初始化横幅。我更改了showBanner函数,并添加了一个新函数来对其进行初始化:

  BannerAd createBannerAd() {
    return BannerAd(
      adUnitId: BannerAd.testAdUnitId,
      size: AdSize.banner,
      targetingInfo: targetingInfo,
      listener: (MobileAdEvent event) {
        print("BannerAd event $event");
      },
    );
  }

  showBanner() {
        _bannerAd ??= createBannerAd();
        _bannerAd
        ..load()
        ..show( 
        // Positions the banner ad 60 pixels from the bottom of the screen
        anchorOffset: 0.0,
        // Positions the banner ad 10 pixels from the center of the screen to the right
        horizontalCenterOffset: 0.0,
        // Banner Position
        // anchorType: AnchorType.bottom,
      );
  }

如果需要,还可以在initState方法内执行此操作:

  @override
  void initState() {
    super.initState();
    FirebaseAdMob.instance.initialize(appId: "YOUR-APP-ID");
    _bannerAd = createBannerAd()..load();
  }
相关问题