有解决这个问题的办法吗?
堆栈跟踪:
[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<…>
这可以使用WidgetsBinding.ensureInitialized()来解决,它建立了Dart层和平台之间的通信。
如果我们需要在调用[runApp]之前初始化绑定,就需要调用这个方法。除非绑定建立,否则颤振不能直接与颤振引擎相互作用。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
/// Your Code which required binding
runApp(
...
)
}
WidgetsFlutterBinding.ensureInitialized()支持各种绑定,例如
ServicesBinding监听平台消息,并将它们定向到接收消息的处理程序(BinaryMessenger)。
PaintingBinding负责绑定到绘画库。
RenderBinding将渲染树绑定到Flutter引擎。
WidgetBinding将小部件树绑定到Flutter引擎。
SchedulerBinding是运行即时任务的调度器。
SemanticsBinding绑定语义层和Flutter引擎。
GestureBinding是手势子系统的绑定。
这些所有的绑定将作为一个胶水之间的Dart和平台。
这可以使用WidgetsBinding.ensureInitialized()来解决,它建立了Dart层和平台之间的通信。
如果我们需要在调用[runApp]之前初始化绑定,就需要调用这个方法。除非绑定建立,否则颤振不能直接与颤振引擎相互作用。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
/// Your Code which required binding
runApp(
...
)
}
WidgetsFlutterBinding.ensureInitialized()支持各种绑定,例如
ServicesBinding监听平台消息,并将它们定向到接收消息的处理程序(BinaryMessenger)。
PaintingBinding负责绑定到绘画库。
RenderBinding将渲染树绑定到Flutter引擎。
WidgetBinding将小部件树绑定到Flutter引擎。
SchedulerBinding是运行即时任务的调度器。
SemanticsBinding绑定语义层和Flutter引擎。
GestureBinding是手势子系统的绑定。
这些所有的绑定将作为一个胶水之间的Dart和平台。