You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter混合应用无法启动求助(含WebView与Google AdMob开屏广告)

问题排查与修复方案

你的代码存在几个关键问题导致应用无法正常启动或广告无法展示,以下是具体问题和修复步骤:

1. 未启动生命周期监听

在main函数中,你创建了AppLifecycleReactor实例,但没有调用listenToAppStateChanges()方法,导致应用生命周期事件无法被监听,广告也就不会在应用切换到前台时触发展示。

修复main.dart:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 等待MobileAds初始化完成
  await MobileAds.instance.initialize();

  late AppLifecycleReactor _appLifecycleReactor;
  AppOpenAdManager appOpenAdManager = AppOpenAdManager()..loadAd();
  _appLifecycleReactor =
      AppLifecycleReactor(appOpenAdManager: appOpenAdManager);
  // 启动生命周期监听
  _appLifecycleReactor.listenToAppStateChanges();

  runApp(
    const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: WebViewApp(),
    ),
  );
}

2. 开屏广告加载完成后未调用展示方法

在AppOpenAdManager的showAdIfAvailable()方法中,你设置了fullScreenContentCallback,但最后没有调用_appOpenAd!.show()来展示广告,导致即使广告加载完成也不会显示。

修复app_open_ad_manager.dart的showAdIfAvailable方法:

void showAdIfAvailable() {
  if (!isAdAvailable) {
    print('Tried to show ad before available.');
    loadAd();
    return;
  }
  if (_isShowingAd) {
    print('Tried to show ad while already showing an ad.');
    return;
  }
  // Set the fullScreenContentCallback and show the ad.
  _appOpenAd!.fullScreenContentCallback = FullScreenContentCallback(
    onAdShowedFullScreenContent: (ad) {
      _isShowingAd = true;
      print('$ad onAdShowedFullScreenContent');
    },
    onAdFailedToShowFullScreenContent: (ad, error) {
      print('$ad onAdFailedToShowFullScreenContent: $error');
      _isShowingAd = false;
      ad.dispose();
      _appOpenAd = null;
    },
    onAdDismissedFullScreenContent: (ad) {
      print('$ad onAdDismissedFullScreenContent');
      _isShowingAd = false;
      ad.dispose();
      _appOpenAd = null;
      loadAd();
    },
  );
  // 新增:展示广告
  _appOpenAd!.show();
}

3. MobileAds初始化未等待完成

原代码中MobileAds.instance.initialize()是异步操作,但没有用await等待初始化完成就加载广告,可能导致广告加载失败。该问题已在第一个修复中通过添加async和await解决。

4. WebView页面冗余的WidgetsBindingObserver配置

_WebViewAppState混入了WidgetsBindingObserver但没有实现必要的didChangeAppLifecycleState方法,会导致编译器警告,建议移除多余配置:

修复WebViewApp的状态类:

class _WebViewAppState extends State<WebViewApp> { // 移除with WidgetsBindingObserver
  late final WebViewController controller;

  @override
  void initState() {
    super.initState();
    // 移除WidgetsBinding.instance.addObserver(this);
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(
        Uri.parse('https://example.com'),
      );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

额外注意事项

  • 确保Android/iOS项目已正确配置AdMob依赖和清单文件(如AndroidManifest.xml、Info.plist),否则广告可能无法加载。
  • 当前代码使用的是官方测试广告ID,测试阶段可正常使用,正式发布需替换为自己的AdMob广告单元ID。

内容的提问来源于stack exchange,提问作者Citadel King

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 23:50:23