小编典典

Flutter:未处理的异常:初始化绑定之前已访问ServicesBinding.defaultBinaryMessenger

flutter

有解决此问题的解决方案吗?

堆栈跟踪:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
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.
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.
#0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:73:7)
#1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:86:4)
#2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
#3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
<asynchronous suspension>
#4      MethodChannel.invokeMapMethod (package:f<…>

阅读 861

收藏
2020-08-13

共1个答案

小编典典

升级Flutter时会引入此问题。这背后的原因是您正在等待一些数据或正在其中运行async函数main()

我正在ScopedModel内部main()和内部进行初始化,以等待一些数据。

有一个很小的修复程序。要做之前就跑进WidgetsFlutterBinding.ensureInitialized()里面。奇迹般有效!!void main()``runApp()

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(Delta(
    model: ProductDataModel(),
  ));
}
2020-08-13