Flutter 添加camera^0.9.8+1运行后报错:Unhandled Exception: MissingPluginException(No implementation found...

记录一次这个报错异常。提示的库是第三方库,前面还有一段异常提示:

I/flutter ( 2851): `camera_android` threw an error: Binding has not yet been initialized.
I/flutter ( 2851): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
I/flutter ( 2851): 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 ( 2851): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
I/flutter ( 2851): 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 ( 2851): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: Binding has not yet been initialized.
E/flutter ( 2851): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter ( 2851): 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 ( 2851): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
E/flutter ( 2851): 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. (package:flutter/src/foundation/binding.dart:281:9)
E/flutter ( 2851): #1      BindingBase.checkInstance (package:flutter/src/foundation/binding.dart:363:6)
E/flutter ( 2851): #2      ServicesBinding.instance (package:flutter/src/services/binding.dart:48:54)
E/flutter ( 2851): #3      MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:387:51)
E/flutter ( 2851): #4      new AndroidCamera (package:camera_android/src/android_camera.dart:26:13)
E/flutter ( 2851): #5      AndroidCamera.registerWith (package:camera_android/src/android_camera.dart:32:31)
E/flutter ( 2851): #6      _PluginRegistrant.register (file:///Users/ufogxl/Documents/FlutterWorkspace/flutter-samples/.dart_tool/flutter_build/dart_plugin_registrant.dart:24:23)
E/flutter ( 2851): 

不仅是camera无法注册运行,强行继续运行后shared_preference也会报错

E/flutter ( 2851): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Binding has not yet been initialized.
E/flutter ( 2851): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter ( 2851): 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 ( 2851): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
E/flutter ( 2851): 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.
E/flutter ( 2851): #0      BindingBase.checkInstance. (package:flutter/src/foundation/binding.dart:281:9)
E/flutter ( 2851): #1      BindingBase.checkInstance (package:flutter/src/foundation/binding.dart:363:6)
E/flutter ( 2851): #2      ServicesBinding.instance (package:flutter/src/services/binding.dart:48:54)
E/flutter ( 2851): #3      MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:387:51)
E/flutter ( 2851): #4      new MethodChannelCamera (package:camera_platform_interface/src/method_channel/method_channel_camera.dart:25:13)
E/flutter ( 2851): #5      CameraPlatform._instance (package:camera_platform_interface/src/platform_interface/camera_platform.dart:27:37)
E/flutter ( 2851): #6      CameraPlatform._instance (package:camera_platform_interface/src/platform_interface/camera_platform.dart)
E/flutter ( 2851): #7      CameraPlatform.instance (package:camera_platform_interface/src/platform_interface/camera_platform.dart:32:41)
E/flutter ( 2851): #8      availableCameras (package:camera/src/camera_controller.dart:27:25)
E/flutter ( 2851): #9      initCamera (package:samples/main.dart:28:9)
E/flutter ( 2851): #10     main (package:samples/main.dart:17:3)
E/flutter ( 2851): #11     _runMainZoned.. (dart:ui/hooks.dart:130:25)
E/flutter ( 2851): #12     _rootRun (dart:async/zone.dart:1426:13)
E/flutter ( 2851): #13     _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 2851): #14     _runZoned (dart:async/zone.dart:1861:10)
E/flutter ( 2851): #15     runZonedGuarded (dart:async/zone.dart:1849:12)
E/flutter ( 2851): #16     _runMainZoned. (dart:ui/hooks.dart:126:5)
E/flutter ( 2851): #17     _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter ( 2851): #18     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
E/flutter ( 2851): 
I/Gralloc4( 2851): Adding additional valid usage bits: 0x546c08202000
E/flutter ( 2851): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

这个问题困扰了好久,尝试过以下几种解决方案:

1.

网上搜到最多的办法都是在main函数中第一行添加如下函数。但是我已经添加过了。


void main() {
    //在main函数第一行添加这句话
    WidgetsFlutterBinding.ensureInitialized();
    runApp(const MyApp());
}

2.

flutter upgrade
flutter clean 
flutter pub get 

重新运行(无效)

3.

卸载重装flutter(无效)

4.

继续查找网上的文章,其中有一篇提到过可能是某个插件有问题后,导致后续插件都无法注册。但是我这边代码都和插件深度结合了,很难按照他的方案每个都注释一遍排查-。-

只尝试了:降低pubspec中camera和shared_preferenc的版本号 -> 执行第二步(无效)


折腾了好久找到了一个新鲜的issue提到这个问题:需要把依赖的插件版本强行改小。

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

使用这个方案终于解决了我的问题!应该还是插件本身的bug导致的。

但是为什么之前降低版本号的方案不起作用呢?我顿时陷入了沉思。。。

最后又把项目的配置文件看了一遍,才找到罪魁祸首:

pubspec.lock

降低pubspec.yaml后运行pub get是不会主动降低pubspec.lock中依赖的版本号的,所以遇到某些和依赖有关的问题需要试试降级是否有效,可以先删除pubspec.yaml中的目标依赖运行pub get后,把依赖的降级版本加入再次运行pub get。

附:pubspec.lock参考文章

你可能感兴趣的:(Flutter 添加camera^0.9.8+1运行后报错:Unhandled Exception: MissingPluginException(No implementation found...)