flutter - Rewarded Video Ads Error when Reload : “ad_not_loaded, show failed for rewarded video, no ad was loaded, null)”

后端 未结 4 1546
无人及你
无人及你 2021-02-20 10:56

i try to reload Rewarded Video Ads, when i call RewardedVideoAd.instance.load(adUnitId: \"xxx\", targetingInfo: xyz); i find below error :

4条回答
  •  再見小時候
    2021-02-20 11:48

    No need for calling set state as it is expensive and causes a full rebuild.

    This problem can easily be fixed in the following way:

    import 'package:flutter/material.dart';
    import 'package:firebase_admob/firebase_admob.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: HomePage());
      }
    }
    
    class HomePage extends StatefulWidget {
      HomePage({Key key}) : super(key: key);
    
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State {
      //The targeting info required for Rewarded Videos Ads
      MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
        keywords: ['flutterio', 'beautiful apps'],
        contentUrl: 'https://flutter.io',
        childDirected: false,
        testDevices: [], // Android emulators are considered test devices
      );
    
      //An instance to be called in the init state
      RewardedVideoAd _videoAd = RewardedVideoAd.instance;
    
      @override
      void initState() {
        //---------------------------------------//
        //Initialise the listener with the values.
        _videoAd.listener =
            (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
          if (event == RewardedVideoAdEvent.completed) {
            //When the video ad gets completed load a new video ad
            _videoAd
                .load(
                    adUnitId: RewardedVideoAd.testAdUnitId,
                    targetingInfo: targetingInfo)
                .catchError((e) => print('Error in loading.'));
          }
    
          //On every other event change pass the values to the _handleEvent Method.
          _handleEvent(event, rewardType, 'Reward', rewardAmount);
        };
        //------------------------------------------------------------------//
    
        //This will load the video when the widget is built for the first time.
        _videoAd
            .load(
                adUnitId: RewardedVideoAd.testAdUnitId,
                targetingInfo: targetingInfo)
            .catchError((e) => print('Error in loading.'));
    
        //-----------------------------------------------------//
        super.initState();
      }
    
      //---- Useful function to know exactly what is being done ----//
      void _handleEvent(RewardedVideoAdEvent event, String rewardType,
          String adType, int rewardAmount) {
        switch (event) {
          case RewardedVideoAdEvent.loaded:
            _showSnackBar('New Admob $adType Ad loaded!', 1500);
            break;
          case RewardedVideoAdEvent.opened:
            _showSnackBar('Admob $adType Ad opened!', 1500);
            break;
          //
          //The way we are fixing the issue is here.
          //This is by calling the video to be loaded when the other rewarded video is closed.
          case RewardedVideoAdEvent.closed:
            _showSnackBar('Admob $adType Ad closed!', 1500);
            _videoAd
                .load(
                    adUnitId: RewardedVideoAd.testAdUnitId,
                    targetingInfo: targetingInfo)
                .catchError((e) => print('Error in loading.'));
            break;
          case RewardedVideoAdEvent.failedToLoad:
            _showSnackBar('Admob $adType failed to load.', 1500);
            break;
          case RewardedVideoAdEvent.rewarded:
            _showSnackBar('Rewarded $rewardAmount', 3000);
            break;
          default:
        }
      }
    
      //Snackbar shown with ad status
      void _showSnackBar(String content, int duration) {
        Scaffold.of(context).showSnackBar(SnackBar(
          content: Text(content),
          duration: Duration(milliseconds: duration),
        ));
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: FlatButton(
              child: Text('Play AD'),
              onPressed: () {
                _videoAd.show().catchError(
                    (e) => print("error in showing ad: ${e.toString()}"));
              },
            ),
          ),
        );
      }
    }
    
    
    

提交回复
热议问题