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

4 浏览
0 Comments

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

有没有解决这个问题的解决方案?

堆栈跟踪:

[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. (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)

#4      MethodChannel.invokeMapMethod (package:f<…>

admin 更改状态以发布 2023年5月21日
0
0 Comments

如果你在等待main()方法,通常情况下会发生这种情况。因此,解决方案如下:

void main() {
  // add this, and it should be the first line in main method
  WidgetsFlutterBinding.ensureInitialized(); 
  // rest of your app code
  runApp(
    MaterialApp(...),
  );
}

0
0 Comments

如果你升级Flutter后遇到这个问题。
原因是您在等待某些数据或在main()函数内运行异步函数。

我在main()中初始化ScopedModel,并在其中等待数据。

有一个很小的修复方法。
只需在runApp()之前,在void main()中运行WidgetsFlutterBinding.ensureInitialized()。非常好用!!

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

0