简体   繁体   English

创建插页式广告时“对空值使用空检查运算符”

[英]'Null check operator used on a null value' while creating Interstitial Ads

I'm trying to add an interstitial ads, but got 'Null check operator used on a null value' error.我正在尝试添加插页式广告,但出现“空值检查运算符用于空值”错误。

Error错误

E/flutter (21082): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value

Code problem代码问题

onTap: () async {
                    isInterstitialVideoAvailable =
                        (await FlutterApplovinMax.isInterstitialLoaded(
                            listener!))!;

                    if (isInterstitialVideoAvailable) {
                      FlutterApplovinMax.showInterstitialVideo(
                          (AppLovinAdListener? event) => listener!(event));
                    }

Main code involved涉及的主要代码

class CollectionCard extends StatefulWidget {
  const CollectionCard();

  @override
  State<CollectionCard> createState() => _CollectionCardState();
}

class _CollectionCardState extends State<CollectionCard> {
  AppLovinListener? get listener => null;

  // AppLovinListener get listener => null;

  void initState() {
    super.initState();
    FlutterApplovinMax.initInterstitialAd('91b26a5859e1b480');
  }

  bool isInterstitialVideoAvailable = false;

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;

    /*24 is for notifications bar on Android */
    final double itemHeight = (size.height - kToolbarHeight - 28) / 2;
    final double itemWidth = size.width / 4;

    // var len = listener?.length ?? 0;

    return Container(
        child: Padding(
      padding: const EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 10.0),
      child: Column(
        children: <Widget>[
          GridView.count(
            primary: true,
            padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
            crossAxisSpacing: 10, //Reduce Horizontal Spacing
            mainAxisSpacing: 10, //Reduce Vertical Spacing
            crossAxisCount: 3,
            physics: ScrollPhysics(),
            childAspectRatio: (6 / 8),
            // (itemWidth / itemHeight),
            shrinkWrap: true,
            children: <Widget>[
              Card(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(15.0),
                ),
                elevation: 2,
                color: Theme.of(context).scaffoldBackgroundColor,
                child: InkWell(
                   onTap: () async {
                    isInterstitialVideoAvailable =
                        (await FlutterApplovinMax.isInterstitialLoaded(
                            listener!))!;

                    if (isInterstitialVideoAvailable) {
                      FlutterApplovinMax.showInterstitialVideo(
                          (AppLovinAdListener? event) => listener!(event));
                    }


                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (ctx) => LearnPage(),
                      ),
                    );
                  },
                  child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        ImageIcon(
                          AssetImage('assets/icons/learn.png'),
                          color: kLightPrimary,
                          size: 60,
                        ), // Icon(
                        //   layout.icon,
                        //   size: 40,
                        // ),
                        Text(
                          'Learn',
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),

Can anyone provide a solution for this?任何人都可以为此提供解决方案吗?

You have to init a Interstitial Ads before use it,您必须在使用前启动插页式广告,

Example:例子:

Init Ads on initState()在 initState() 上初始化广告

InterstitialAd? _interstitialAd;
int _countMaxAdFailedToLoad = 3;
int _countAdInitialToLoad = 0;

@override
void initState() {
  super.initState();
  _createAds();
}

void _createAds(){
  InterstitialAd.load(
    adUnitId: Platform.isAndroid 
      ? 'ca-app-pub-3940256099942544/1033173712' 
      : 'ca-app-pub-3940256099942544/4411468910',
    request: AdRequest(),
    adLoadCallback: InterstitialAdLoadCallback(
      onAdLoaded: (InterstitialAd ad) {
        _interstitialAd = ad;
        _countAdInitialToLoad = 0;
      },
      onAdFailedToLoad: (LoadAdError error) {
        print('InterstitialAd failed to load: $error');
        _countAdInitialToLoad += 1;
        _interstitialAd = null;
        if (_countAdInitialToLoad >= _countMaxAdFailedToLoad) {
          _createInterstitialAds();
        }
      },
    ),
  );
}

And you can show this ads.你可以展示这个广告。

onTap: (){
if (_interstitialAd != null) {
   _interstitialAd?.fullScreenContentCallback =
       FullScreenContentCallback(
         onAdDismissedFullScreenContent: (InterstitialAd ad) {
           print('$ad onAdDismissedFullScreenContent.');
           ad.dispose();
           _createAds();
        }, 
         onAdFailedToShowFullScreenContent:
           (InterstitialAd ad, AdError error) {
           print('$ad onAdFailedToShowFullScreenContent: $error');
           ad.dispose();
           _createAds();
              });
     _interstitialAd?.show();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM