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

Flutter集成google_mobile_ads遇WriteBuffer/ReadBuffer未找到错误求助

解决google_mobile_ads插件的WriteBuffer/ReadBuffer未找到错误

你的错误是因为google_mobile_ads 3.0.0版本与当前Flutter版本不兼容,旧插件依赖的WriteBuffer和ReadBuffer类在新版Flutter中已被调整或移除,按以下步骤解决:

  • 升级插件到最新稳定版
    打开项目的pubspec.yaml,将google_mobile_ads的版本号更新为最新稳定版(可查看pub.dev的最新版本),比如:

    dependencies:
      google_mobile_ads: ^4.0.0 # 替换为实际最新版本
    

    然后执行命令更新依赖:

    flutter pub get
    

    或者直接用命令升级:

    flutter pub upgrade google_mobile_ads
    
  • 确保Flutter版本兼容
    运行flutter doctor检查当前Flutter版本,若版本过旧,执行升级命令:

    flutter upgrade
    

    插件官方文档会标注支持的Flutter版本范围,确保两者匹配。

  • 清理缓存并重建项目
    缓存问题也可能导致此类错误,依次执行:

    flutter clean
    flutter pub get
    flutter run
    
  • 适配示例代码到新版插件
    你使用的示例代码是针对旧版插件写的,新版google_mobile_ads的插屏广告API有调整,以下是新版的基础实现示例:

    import 'package:flutter/material.dart';
    import 'package:google_mobile_ads/google_mobile_ads.dart';
    
    class InterstitialAdDemo extends StatefulWidget {
      const InterstitialAdDemo({super.key});
    
      @override
      State<InterstitialAdDemo> createState() => _InterstitialAdDemoState();
    }
    
    class _InterstitialAdDemoState extends State<InterstitialAdDemo> {
      InterstitialAd? _interstitialAd;
      bool _isAdLoaded = false;
    
      @override
      void initState() {
        super.initState();
        _loadInterstitialAd();
      }
    
      void _loadInterstitialAd() {
        InterstitialAd.load(
          adUnitId: 'ca-app-pub-3940256099942544/1033173712', // 测试ID,替换为你的正式广告ID
          request: const AdRequest(),
          adLoadCallback: InterstitialAdLoadCallback(
            onAdLoaded: (ad) {
              setState(() {
                _interstitialAd = ad;
                _isAdLoaded = true;
              });
              // 监听广告生命周期事件
              _interstitialAd?.fullScreenContentCallback = FullScreenContentCallback(
                onAdDismissedFullScreenContent: (ad) {
                  ad.dispose();
                  _loadInterstitialAd(); // 加载下一个广告
                },
                onAdFailedToShowFullScreenContent: (ad, error) {
                  ad.dispose();
                  _loadInterstitialAd();
                },
              );
            },
            onAdFailedToLoad: (error) {
              setState(() {
                _isAdLoaded = false;
              });
              print('插屏广告加载失败: ${error.message}');
            },
          ),
        );
      }
    
      void _showAd() {
        if (_isAdLoaded && _interstitialAd != null) {
          _interstitialAd?.show();
        } else {
          ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(content: Text('广告未加载完成,请稍后重试')),
          );
        }
      }
    
      @override
      void dispose() {
        _interstitialAd?.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('插屏广告示例')),
          body: Center(
            child: ElevatedButton(
              onPressed: _showAd,
              child: const Text('展示插屏广告'),
            ),
          ),
        );
      }
    }
    

内容的提问来源于stack exchange,提问作者Manh Do

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 23:40:31