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

275

有什么解决这个问题的方案吗?

堆栈跟踪:

[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<…>

1
请在main()函数中将以下代码行作为第一条语句 - WidgetsFlutterBinding.ensureInitialized(); - Vijay Ram
13个回答

1

我之前使用的是版本号为v1.12.13+hotfix.5,后来切换到了v1.14.4版本,这样就可以正常工作了。

错误提示说你应该添加WidgetsFlutterBinding.ensureInitialized();,但是对于我来说这并没有起作用,所以我选择了另一个版本。但需要注意的一点是,在你的主文件中,你仍然需要将WidgetsFlutterBinding.ensureInitialized();作为第一行代码添加进去!


0

0
一个可能的解决方案是关闭Flutter应用程序,然后重新启动它。
当我执行flutter pub add shared_preferences时,我的Web浏览器尝试进行热重载并失败了。停止调试会话并启动一个新的会话,一切都正常运行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接