Flutter:未处理的异常:初始化绑定之前已访问ServicesBinding.defaultBinaryMessenger [英] Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized
本文介绍了Flutter:未处理的异常:初始化绑定之前已访问ServicesBinding.defaultBinaryMessenger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有解决此问题的解决方案吗?
Any solution to solve this problem?
堆栈跟踪:
[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<…>
推荐答案
升级Flutter时会引入此问题.
造成这种情况的原因是您正在等待某些数据或正在main()
中运行async
函数.
This problem is introduced when you upgrade Flutter.
The reason behind this is you are waiting for some data or running an async
function inside main()
.
我正在main()
内部初始化ScopedModel
,正在等待一些数据.
I was initialising ScopedModel
inside main()
and inside that I was awaiting for some data.
有一个很小的修复程序.
在执行runApp()
之前,只需在void main()
内部运行WidgetsFlutterBinding.ensureInitialized()
.就像魅力一样!!
There is a very small fix.
Just run WidgetsFlutterBinding.ensureInitialized()
inside void main()
, before you do runApp()
. Works like a charm!!
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(Delta(
model: ProductDataModel(),
));
}
这篇关于Flutter:未处理的异常:初始化绑定之前已访问ServicesBinding.defaultBinaryMessenger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文