简体   繁体   English

应该使用 WidgetsFlutterBinding.ensureInitialized(); 两次?

[英]should iuse WidgetsFlutterBinding.ensureInitialized(); two times?

I have used firebase and shared_preferences in the main function so I should use the instruction WidgetsFlutterBinding.ensureInitialized();我在主 function 中使用了firebaseshared_preferences所以我应该使用指令 WidgetsFlutterBinding.ensureInitialized(); two times or one enough?两次还是一次就够了?

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences prefs = await SharedPreferences.getInstance();
 .....
  runApp( MyApp());
}

One time initialization is enough.一次初始化就足够了。

If you look at ensureInitialized() 's source code,如果您查看ensureInitialized()的源代码,
all it does is to check whether the WidgetsBinding instance is null or not.它所做的只是检查WidgetsBinding实例是否为null
If it's null , a new instance is created.如果是null ,则创建一个新实例。

Here's the function:这是 function:

    static WidgetsBinding ensureInitialized() {
      if (WidgetsBinding.instance == null)
        WidgetsFlutterBinding();
      return WidgetsBinding.instance!;
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM