[英]What is WidgetsFlutterBinding and how it is being used in Flutter app?
When and how we use it?我们何时以及如何使用它? How it works?
这个怎么运作?
You have to use it, in this way:您必须以这种方式使用它:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
https://flutter.dev/docs/resources/architectural-overview#architectural-layers https://flutter.dev/docs/resources/architectural-overview#architectural-layers
The WidgetFlutterBinding is used to interact with the Flutter engine. WidgetFlutterBinding 用于与 Flutter 引擎交互。 Firebase.initializeApp() needs to call native code to initialize Firebase, and since the plugin needs to use platform channels to call the native code, which is done asynchronously therefore you have to call ensureInitialized() to make sure that you have an instance of the WidgetsBinding.
Firebase.initializeApp() 需要调用原生代码来初始化 Firebase,由于插件需要使用平台通道来调用原生代码,这是异步完成的,所以你必须调用 ensureInitialized() 来确保你有一个实例小部件绑定。
Answerd By https://stackoverflow.com/users/7015400/peter-haddad由https 回答://stackoverflow.com/users/7015400/peter-haddad
Answer Link https://stackoverflow.com/a/63873689答案链接https://stackoverflow.com/a/63873689
WidgetsBinding.ensureInitialized()
This initialised communication between the Dart Layer
and Flutter Engine
. WidgetsBinding.ensureInitialized()
Dart Layer
和Flutter Engine
之间的初始化通信。
We need to call this method if we need the binding to be initialised before calling [runApp].如果我们需要在调用 [runApp] 之前初始化绑定,我们需要调用此方法。 Flutter cannot directly interact with flutter engine until and unless binding is established.
Flutter 不能直接与 flutter 引擎交互,除非建立绑定。
Example 1: Shows Firebase platform initialisation between flutter and native code, which Firestore
class do internally.示例 1:显示 flutter 和本机代码之间的 Firebase 平台初始化,
Firestore
class 在内部执行。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firestore.initializeApp();
runApp(
...
)
}
OR或者
Example 2: Shows device orientation changes before even app starts, for this also we need to established binding connection.示例 2:在应用程序启动之前显示设备方向变化,为此我们还需要建立绑定连接。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(
...
)
}
Internally, WidgetsBinding.ensureInitialized()
supports various binding likes GestureBinding
, SchedulerBinding
, ServicesBinding
, PaintingBinding
, SemanticsBinding
, RendererBinding
, WidgetsBinding
在内部,
WidgetsBinding.ensureInitialized()
支持各种绑定,例如GestureBinding
、 SchedulerBinding
、 ServicesBinding
、 PaintingBinding
、 SemanticsBinding
、 RendererBinding
、 WidgetsBinding
ServicesBinding
listens for platform messages and directs them to the handler for incoming messages (BinaryMessenger). ServicesBinding
侦听平台消息并将它们定向到传入消息的处理程序 (BinaryMessenger)。
PaintingBinding
is responsible for binding to the painting library. PaintingBinding
负责绑定到绘画库。
RenderBinding
binds the render tree to the Flutter engine. RenderBinding
将渲染树绑定到 Flutter 引擎。
WidgetBinding
binds the widget tree to the Flutter engine. WidgetBinding
将小部件树绑定到 Flutter 引擎。
SchedulerBinding
is the scheduler for running immediate tasks. SchedulerBinding
是用于运行即时任务的调度程序。
SemanticsBinding
binds the semantics layer and the Flutter engine. SemanticsBinding
绑定了语义层和 Flutter 引擎。
GestureBinding
is a binding for the gesture subsystem. GestureBinding
是手势子系统的绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.