Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is WidgetsFlutterBinding and how it is being used in Flutter app?

Tags:

flutter

When and how we use it? How it works?

WidgetsFlutterBinding

like image 1000
mjsid Avatar asked Oct 15 '22 03:10

mjsid


People also ask

What is WidgetsFlutterBinding in Flutter?

WidgetsFlutterBinding class Null safety. A concrete binding for applications based on the Widgets framework. This is the glue that binds the framework to the Flutter engine. When using the widgets framework, this binding, or one that implements the same interfaces, must be used.

What is the use of WidgetsFlutterBinding ensureInitialized?

ensureInitialized method Null safetyReturns an instance of the binding that implements WidgetsBinding. If no binding has yet been initialized, the WidgetsFlutterBinding class is used to create and initialize one. You only need to call this method if you need the binding to be initialized before calling runApp.

What is the purpose of embedder layer in Flutter architecture?

Embedder layer uses the engine layer as a library. It is the start point of a Flutter application when launched. It is written in the platform-specific language and hosts the Flutter engine. Embedder enables communication with the underlying operating system, obtains threads for UI, and provides texture.


1 Answers

You have to use it, in this way:

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

https://flutter.dev/docs/resources/architectural-overview#architectural-layers

The WidgetFlutterBinding is used to interact with the Flutter engine. Firebase.initializeApp() needs to call native code to initialize Firebase, and since the plugin needs to use platform channels to call the native code, which is done asynchronously therefore you have to call ensureInitialized() to make sure that you have an instance of the WidgetsBinding.

Answerd By https://stackoverflow.com/users/7015400/peter-haddad

Answer Link https://stackoverflow.com/a/63873689

like image 51
saif aly Avatar answered Oct 19 '22 00:10

saif aly