How to stop app from crashing due to admob(flutter, iOS)?

Viewed 21

I tried implementing admob to my flutter app. I used open ads to show an ad on launch, but whenever I launch the app it always crashes. Maybe I implemented the code in Info.plist wrong?

enter image description here

enter image description here

  AppOpenAd? openAd;
Future loadAd() async{
  await AppOpenAd.load(
    adUnitId: 'ca-app-pub-######', 
    request: const AdManagerAdRequest(),
    adLoadCallback: AppOpenAdLoadCallback(
    onAdLoaded: (ad){
      print('ad is loaded');
      openAd = ad;
      openAd!.show();
    }, 
    onAdFailedToLoad: (error){
      print('ad failed to load $error');
    }), 
    orientation: AppOpenAd.orientationPortrait
    );
}
void showAd() {
  if(openAd==null){
    print('show before loading');
    loadAd();
    return;
  }
  openAd!.fullScreenContentCallback = FullScreenContentCallback(
    onAdShowedFullScreenContent: (ad){
      print('onAdShowedFullScreenContent');
    },
    onAdFailedToShowFullScreenContent: (ad, error){
      ad.dispose();
      print('failed to load $error');
      openAd = null;
      loadAd();
    }, onAdDismissedFullScreenContent: (ad){
      ad.dispose();
      print('dismissed');
      openAd = null;
      loadAd();
    }
  );
  openAd!.show();
}
Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await MobileAds.instance.initialize();
  await loadAd();
  final SharedPreferences prefs = await SharedPreferences.getInstance();

  runApp(WhatsTheWord());
}
0 Answers
Related