简体   繁体   English

什么是 WidgetsFlutterBinding 以及它是如何在 Flutter 应用程序中使用的?

[英]What is WidgetsFlutterBinding and how it is being used in Flutter app?

When and how we use it?我们何时以及如何使用它? How it works?这个怎么运作?

WidgetsFlutterBinding WidgetsFlutterBinding

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-haddadhttps 回答://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 LayerFlutter 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()支持各种绑定,例如GestureBindingSchedulerBindingServicesBindingPaintingBindingSemanticsBindingRendererBindingWidgetsBinding

  1. ServicesBinding listens for platform messages and directs them to the handler for incoming messages (BinaryMessenger). ServicesBinding侦听平台消息并将它们定向到传入消息的处理程序 (BinaryMessenger)。

  2. PaintingBinding is responsible for binding to the painting library. PaintingBinding负责绑定到绘画库。

  3. RenderBinding binds the render tree to the Flutter engine. RenderBinding将渲染树绑定到 Flutter 引擎。

  4. WidgetBinding binds the widget tree to the Flutter engine. WidgetBinding将小部件树绑定到 Flutter 引擎。

  5. SchedulerBinding is the scheduler for running immediate tasks. SchedulerBinding是用于运行即时任务的调度程序。

  6. SemanticsBinding binds the semantics layer and the Flutter engine. SemanticsBinding绑定了语义层和 Flutter 引擎。

  7. GestureBinding is a binding for the gesture subsystem. GestureBinding是手势子系统的绑定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Web 上未调用 WidgetsFlutterBinding.ensureInitialized 或 Firebase 初始化应用程序 - WidgetsFlutterBinding.ensureInitialized or Firebase initialize app is not called on Flutter web Flutter 更新小部件FlutterBinding - Flutter Update WidgetsFlutterBinding 如何获取 Flutter App 中使用的包版本 - How to get Package Version being used in Flutter App 当应用程序以 WidgetsFlutterBinding.ensureInitialized() 启动时,Flutter 模拟时钟(使用 withClock)的行为不一致 - Flutter mocked clock (using withClock) behave inconsistenly when app starts with WidgetsFlutterBinding.ensureInitialized() Flutter 应用程序正在使用时的后台任务 - Flutter background task when app is being used WidgetsFlutterBinding.ensureInitialized() 有什么作用? - What Does WidgetsFlutterBinding.ensureInitialized() do? 隔离未处理的异常:E/flutter 调用`WidgetsFlutterBinding.ensureInitialized()` - Isolate Unhandled exception: E/flutter call the `WidgetsFlutterBinding.ensureInitialized()` Flutter WidgetsFlutterBinding.ensureInitialized() 在处理隔离内的 rootBundle 时出现 - Flutter WidgetsFlutterBinding.ensureInitialized() appears when dealing with rootBundle inside an isolate 如果 Flutter App 使用的某些软件包已停产或更改,会发生什么情况? - What happens to the Flutter App if some packages used by it is discontinued or changed? Flutter:用作package时如何获取本地AssetBundle? - Flutter: How to get the local AssetBundle when being used as a package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM