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());
}
CodePudding user response:
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!;
}