Flutter 在颤振中强制纵向定向时出错
我试图强制我的颤振应用程序只使用肖像模式。我的main()方法如下所示Flutter 在颤振中强制纵向定向时出错,flutter,Flutter,我试图强制我的颤振应用程序只使用肖像模式。我的main()方法如下所示 void main() { SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle); SystemChrome.setPreferredOrientations(ALLOWED_ORIENTATIONS) .then((_) => runApp(MyApp())); } 以下是允许的_方向的定义: const List<DeviceOrientati
void main() {
SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle);
SystemChrome.setPreferredOrientations(ALLOWED_ORIENTATIONS)
.then((_) => runApp(MyApp()));
}
以下是允许的_方向的定义:
const List<DeviceOrientation> ALLOWED_ORIENTATIONS = [
DeviceOrientation.portraitUp
];
看起来有一些比赛条件正在失败。我只是不确定是什么原因造成的。删除SystemChrome.setPreferredOrientations()
行会使应用程序按预期编译。所以,我认为这个错误与这条线有关
有什么建议吗?就像错误所说的那样
如果您正在运行应用程序并且需要访问二进制文件
调用runApp()
之前的messenger(例如,在
插件初始化),然后需要显式调用
WidgetsFlutterBinding.ensureInitialized()
首先
Future main()异步{
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.SetSystemTimeIOVerlayStyle(uiOverlayStyle);
等待SystemChrome.setPreferredOrientations(允许的方向);
runApp(MyApp());
}
它仍在告诉我错误。这是否回答了您的问题?
[VERBOSE-2:ui_dart_state.cc(157)] 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.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle);
await SystemChrome.setPreferredOrientations(ALLOWED_ORIENTATIONS);
runApp(MyApp());
}