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

Flutter升级插件后启动报camera_android绑定未初始化错误

问题概述

升级项目依赖插件后应用无法正常启动,升级前所有功能运行正常。

运行环境
  • Flutter分支:stable渠道,已更新至当时最新版本
  • 核心版本信息:
    • Flutter 3.0.2 • channel stable
    • Framework • revision cd41fdd(2022-06-08 09:52:13 -0700发布)
    • Engine • revision f15f824b57
    • 配套工具:Dart 2.17.3、DevTools 2.12.2
报错日志
I/flutter ( 6677): `camera_android` threw an error: Binding has not yet been initialized.
I/flutter ( 6677): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
I/flutter ( 6677): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
I/flutter ( 6677): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
I/flutter ( 6677): If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter.. The app may not function as expected until you remove this plugin from pubspec.yaml
E/flutter ( 6677): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: Binding has not yet been initialized.
E/flutter ( 6677): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter ( 6677): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
E/flutter ( 6677): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
E/flutter ( 6677): If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter., stack trace: #0      BindingBase.checkInstance.<anonymous closure> (package:flutter/src/foundation/binding.dart:281:9)
E/flutter ( 6677): #1      BindingBase.checkInstance (package:flutter/src/foundation/binding.dart:363:6)
E/flutter ( 6677): #2      ServicesBinding.instance (package:flutter/src/services/binding.dart:48:54)
E/flutter ( 6677): #3      MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:387:51)
E/flutter ( 6677): #4      new AndroidCamera (package:camera_android/src/android_camera.dart:26:13)
E/flutter ( 6677): #5      AndroidCamera.registerWith (package:camera_android/src/android_camera.dart:32:31)
E/flutter ( 6677): #6      _PluginRegistrant.register (file:///Users/manish/Documents/REBORN/flutter/user-app/.dart_tool/flutter_build/dart_plugin_registrant.dart:47:23)
根因分析

从报错栈可以直接定位触发点:camera_android插件在自动注册阶段,直接访问ServicesBinding.instance设置方法通道回调,但此时Flutter的基础绑定还未完成初始化。
Flutter 3.0版本调整了插件自动注册的时序,拆分后的camera_android实现插件早期版本没有做兼容处理,在绑定初始化前就调用了绑定实例的getter,直接抛出异常。

解决步骤
  • 优先修复入口初始化逻辑
    打开项目的main.dart文件,在main函数最开头、所有其他逻辑执行前,添加强制初始化绑定的代码,示例:
    void main() {
      // 必须作为第一行执行,提前完成Flutter基础绑定初始化
      WidgetsFlutterBinding.ensureInitialized();
      // 原有业务逻辑,比如初始化第三方插件、执行runApp等
      runApp(const MyApp());
    }
    

    该方法是幂等的,多次调用不会产生副作用,无需担心重复执行问题。

  • 校验依赖版本兼容性
    如果添加上述代码后仍报错,说明当前拉取的camera插件版本和Flutter 3.0.2存在兼容问题,打开pubspec.yaml调整依赖:
    1. 不要单独引入camera_android、camera_avfoundation等平台实现包,只保留主camera依赖即可
    2. 锁定camera版本为适配Flutter 3.0的稳定版,不要用版本范围自动拉取最新不兼容版本,参考配置:
    dependencies:
      flutter:
        sdk: flutter
      camera: 0.9.8+1
    
    修改完成后执行清理命令重新拉取依赖:
    flutter clean
    flutter pub get
    
  • 临时降级规避
    如果上述方案都不生效,直接将camera插件版本回退到升级前项目中正常运行的版本号,重新拉取依赖后编译即可,后续等camera插件官方修复注册时序兼容问题后再尝试升级。
  • 测试场景适配
    如果是在单元测试/Widget测试中遇到同类报错,将测试文件main函数的第一行替换为TestWidgetsFlutterBinding.ensureInitialized();即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 11:18:08