gpt4 book ai didi

firebase - WidgetsFlutterBinding.ensureInitialized() 有什么作用?

转载 作者:行者123 更新时间:2023-12-03 02:44:05 25 4
gpt4 key购买 nike

我正在尝试使用带有以下代码行的 Firebase 包。
我真的很想知道这行代码实际上是做什么的?
官方文档对我帮助不大。有人可以解释一下吗?
Code snippet

最佳答案

您必须以这种方式使用它:

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
enter image description here
https://flutter.dev/docs/resources/architectural-overview#architectural-layers
上图是Flutter的架构层, WidgetFlutterBinding用于与 Flutter 引擎交互。 Firebase.initializeApp()需要调用 native 代码来初始化Firebase,并且由于插件需要使用平台 channel 来调用 native 代码,这是异步完成的,因此您必须调用 ensureInitialized()确保你有一个 WidgetsBinding 的实例.
来自 docs :

Returns an instance of the WidgetsBinding, creating and initializing it if necessary. If one is created, it will be a WidgetsFlutterBinding. If one was previously initialized, then it will at least implement WidgetsBinding.


You only need to call this method if you need the binding to be initialized before calling runApp.



来自 source code :
  @override
Future<FirebaseAppPlatform> initializeApp(
{String name, FirebaseOptions options}) async {
if (name == defaultFirebaseAppName) {
throw noDefaultAppInitialization();
}

// Ensure that core has been initialized on the first usage of
// initializeApp
if (!isCoreInitialized) {
await _initializeCore();
}

// If no name is provided, attempt to get the default Firebase app instance.
// If no instance is available, the user has not set up Firebase correctly for
// their platform.
if (name == null) {
MethodChannelFirebaseApp defaultApp =
appInstances[defaultFirebaseAppName];

if (defaultApp == null) {
throw coreNotInitialized();
}

return appInstances[defaultFirebaseAppName];
}

assert(options != null,
"FirebaseOptions cannot be null when creating a secondary Firebase app.");

// Check whether the app has already been initialized
if (appInstances.containsKey(name)) {
throw duplicateApp(name);
}

_initializeFirebaseAppFromMap(await channel.invokeMapMethod(
'Firebase#initializeApp',
<String, dynamic>{'appName': name, 'options': options.asMap},
));

return appInstances[name];
}
invokeMapMethod将使用指定的参数调用上述 channel 上的方法,然后调用 initializeApp() native 代码中的方法,
https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_core/firebase_core/android/src/main/java/io/flutter/plugins/firebase/core/FlutterFirebaseCorePlugin.java#L227

还有多种初始化 Firebase 的方法,您可以在此处查看:
No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase
在其他方面我们不打电话 WidgetsFlutterBinding.ensureInitialized()runApp()函数在内部调用它:
void runApp(Widget app) {
WidgetsFlutterBinding.ensureInitialized()
..scheduleAttachRootWidget(app)
..scheduleWarmUpFrame();
}
https://github.com/flutter/flutter/blob/bbfbf1770c/packages/flutter/lib/src/widgets/binding.dart#L1012

关于firebase - WidgetsFlutterBinding.ensureInitialized() 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63873338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com