0

I have used firebase and shared_preferences in the main function so I should use the instruction WidgetsFlutterBinding.ensureInitialized(); two times or one enough?

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences prefs = await SharedPreferences.getInstance();
 .....
  runApp( MyApp());
}
  • No only one time, check this https://stackoverflow.com/questions/63873338/what-does-widgetsflutterbinding-ensureinitialized-do/63873689#63873689 – Peter Haddad Dec 30 '21 at 16:17

1 Answers1

1

One time initialization is enough.

If you look at ensureInitialized()'s source code,
all it does is to check whether the WidgetsBinding instance is null or not.
If it's null, a new instance is created.

Here's the function:

    static WidgetsBinding ensureInitialized() {
      if (WidgetsBinding.instance == null)
        WidgetsFlutterBinding();
      return WidgetsBinding.instance!;
    }
Darshan
  • 4,020
  • 2
  • 18
  • 49