Flutter ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.

2022-06-10 14:35:00 浏览数 (1)

flutter sdk升级遇到的异常:

代码语言:javascript复制
E/flutter (17545): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter (17545): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter (17545): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter (17545): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter (17545): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter (17545): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter (17545): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter (17545): #4      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:349:48)
E/flutter (17545): #5      SharedPreferences._getSharedPreferencesMap (package:shared_preferences/shared_preferences.dart:158:25)
E/flutter (17545): #6      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:25:17)
E/flutter (17545): #7      getTheme (package:wanandroid_flutter/main.dart:41:50)
E/flutter (17545): #8      main (package:wanandroid_flutter/main.dart:32:26)
E/flutter (17545): #9      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter (17545): #10     _rootRun (dart:async/zone.dart:1126:13)
E/flutter (17545): #11     _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (17545): #12     _runZoned (dart:async/zone.dart:1518:10)
E/flutter (17545): #13     runZoned (dart:async/zone.dart:1502:12)
E/flutter (17545): #14     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (17545): #15     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (17545): #16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter (17545): 
Lost connection to device.

不要慌,这里着重看一下第二行:

代码语言:javascript复制
If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.   yechaoa

大意:在runApp()之前如果访问二进制文件或者初始化插件,需要先调用WidgetsFlutterBinding.ensureInitialized()

ok,那就照着来吧

代码语言:javascript复制
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

WidgetsFlutterBinding.ensureInitialized();这一行代码到底干啥的呢,WidgetsFlutterBinding字面意思呢,Widget和Flutter绑定,追溯一下源码看看:

代码语言:javascript复制
/// A concrete binding for applications based on the Widgets framework.
///
/// This is the glue that binds the framework to the Flutter engine.
class WidgetsFlutterBinding extends BindingBase with GestureBinding, ServicesBinding, SchedulerBinding, PaintingBinding, SemanticsBinding, RendererBinding, WidgetsBinding {

  /// Returns an instance of the [WidgetsBinding], creating and
  /// initializing it if necessary. If one is created, it will be a
  /// [WidgetsFlutterBinding]. If one was previously initialized, then
  /// it will at least implement [WidgetsBinding].
  ///
  /// You only need to call this method if you need the binding to be
  /// initialized before calling [runApp].
  ///
  /// In the `flutter_test` framework, [testWidgets] initializes the
  /// binding instance to a [TestWidgetsFlutterBinding], not a
  /// [WidgetsFlutterBinding].
  static WidgetsBinding ensureInitialized() {
    if (WidgetsBinding.instance == null)
      WidgetsFlutterBinding();
    return WidgetsBinding.instance;
  }
}

继承自BindingBase ,然后还有一堆手势绑定、服务绑定什么的,一看就是初始化操作,然后看中间有一段注释:

You only need to call this method if you need the binding to be initialized before calling [runApp].

不细翻译了,大意就是在需要的时候调用,那什么时候需要呢,回到开头,在访问二进制文件或者初始化插件的时候,需要在runApp()之前调用WidgetsFlutterBinding.ensureInitialized()

ok,结束。

0 人点赞