Flutter:"camera_android"引发错误:绑定尚未初始化



回归。升级插件后,我的应用程序不再启动。

这是我的颤振规格:

Flutter已经是最新的通道稳定

Flutter 3.0.2•通道稳定•https://github.com/flutter/flutter.git

框架•修订版cd41fdd(10天前)•2022-06-08 09:52:13-0700

发动机•修订版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)
E/flutter ( 6677): 

为我工作

dependency_overrides:
camera_android: 0.9.7+1
camera_avfoundation: 0.9.7+1

是的,他们发布了0.9.8+1作为一个破碎的构建。

https://github.com/flutter/flutter/issues/106236#issuecomment-1161100481

这是转移到按包平台渠道的意外副作用。默认方法通道没有这个问题,因为静态类字段在Dart中是延迟求值的,所以默认方法通道类构造函数只有在实际使用相机时才运行,而新版本的构造函数是在registerWith()中显式触发的。我们需要在这些实现中延迟反向通道的创建,而不是在构造函数中进行设置。

推荐

在他们修复构建之前,我建议:

https://github.com/flutter/flutter/issues/106236#issuecomment-1161149799

  1. 删除pubspec.lock
  2. 降级到0.9.7+1(版本0.9。80.9.8。1抛出错误)
    https://pub.dev/packages/camera/versions
  3. 运行flutter pub get

完全不推荐

另一个解决方案是覆盖依赖

https://github.com/flutter/flutter/issues/106236#issuecomment-1159447210

dependency_overrides:
camera_android: 0.9.7+1
camera_avfoundation: 0.9.7+1

我不推荐这种方法,因为(a)构建已损坏,这是一种可能无法解决所有问题的破解方法,(b)这些额外的代码行会减慢构建速度并添加到存储库历史记录中,(c)您需要记住删除这些代码行。

Flutter团队也不建议采用这种方法。它们将超控码称为"超控码";反模式[s]">
https://github.com/flutter/flutter/issues/106236#issuecomment-1162916231https://github.com/flutter/flutter/issues/106236#issuecomment-1162941742

不是答案,但这个解决方法帮助了我(在pubspec.yaml中):

dependency_overrides:
camera: <=0.9.7

将其添加到您的pubspec.yaml文件中

dependency_overrides:
camera_android: 0.9.7+1
camera_avfoundation: 0.9.7+1

然后在你的终端运行:

flutter clean 
flutter pub get
flutter run

注意,你会在你的终端中看到一些与此覆盖相关的警告,但现在可以忽略它们。

更多关于这个问题的参考,你可以在Github 上查看这个问题

最新更新