sqflite requires WidgetsFlutterBinding.ensureInitialized() but not xmpp_plugin, shared_preferences or device_info_plus ? As per my knowledge plugins require platform specific channels due to which WidgetsFlutterBinding.ensureInitialized() is placed in main() function of flutter app.
CodePudding user response:
You are correct that the WidgetsFlutterBinding.ensureInitialized() method is required by some plugins, such as sqflite, because they require access to platform-specific channels in order to function properly. This is why the ensureInitialized() method is often placed in the main() function of a Flutter app.
However, not all plugins require the ensureInitialized() method. For example, the shared_preferences and device_info_plus plugins do not require access to platform-specific channels, so they do not need the ensureInitialized() method to be called. The xmpp_plugin may or may not require the ensureInitialized() method depending on its specific implementation and the features it uses.
In general, it is a good practice to call the ensureInitialized() method in the main() function of a Flutter app if any of the plugins used by the app require it. This ensures that the app is properly initialized and all the necessary platform-specific channels are set up before the app starts running.
CodePudding user response:
Most plugins should not require WidgetsFlutterBinding.ensureInitialized
because the WidgetsFlutterBinding
instance normally is initialized automatically. Some plugins require that it be explicitly called because they need the instance to be initialized earlier.
From the WidgetsFlutterBinding.ensureInitialized
documentation:
You only need to call this method if you need the binding to be initialized before calling
runApp
.