Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between initState and a class constructor in Flutter?

I read the documentation but it is not clear.

It states "[initState is] Called when this object is inserted into the tree."

When a widget is inserted into a tree, it means it has been created, which means the class constructor is called. What is the purpose of init? Isn't the constructor's purpose to initialize the class instance?

Thank you guys for your time.

like image 863
Walter M Avatar asked Aug 28 '18 22:08

Walter M


People also ask

What is initState () in flutter?

Flutter – initSate() The initState() is a method that is called when an object for your stateful widget is created and inserted inside the widget tree. It is basically the entry point for the Stateful Widgets.

Is initState called before build flutter?

initState() is a method that is called once when the Stateful Widget is inserted in the widget tree. We generally override this method if we need to do some sort of initialization work like registering a listener because, unlike build(), this method is called once.

What is initState and super initState in flutter?

initState() is a method of class State and it is considered as an important lifecycle method in Flutter. initState() is called only Once and we use it for one time initializations. Example : To initialize data that depends on the specific BuildContext . To initialize data that needs to executed before build() .

Why is initState called twice?

This is a combination of two facts: each route is entirely independent. / do not share state with /login , therefore going from / to /login will trigger an initState on the LoginPage from /login .

What is initstate() in flutter?

Implementations of dispose typically end by calling super.dispose initState () is a method of class State and it is considered as an important lifecycle method in Flutter. initState () is called only Once and we use it for one time initializations. To initialize data that depends on the specific BuildContext.

What is the difference between initstate and initstate() methods?

The difference is in the context of creating a State object which has the initState () method: initState () is called after the object is created and at this point, you have access to the BuildContext or the Stateful Widget to which the State is attached, respectively using the context and the widget properties.

What are stateful widgets in flutter?

There are two types of widgets provided in Flutter. As the name suggests Stateful Widgets are made up of some ‘States’. The initState () is a method that is called when an object for your stateful widget is created and inserted inside the widget tree.

What does Super initstate() do?

super.initState () forwards to the default implementation of the State<T> base class of your widget. If you don't override, the default implementation will not be executed but the widget depends on that to work properly. Credit to @Remi, initState () is a method which is called once when the stateful widget is inserted in the widget tree.


2 Answers

The difference is (in the context of creating a State object) which has the initState() method:

  • constructor simply create a new State instance

  • initState() is called after the object is created and at this point you have access to the BuildContext or the StatefulWidget to which the State is attached to, respectively using the context and the widget properties. At this point the State is already mounted.

Reference State: https://api.flutter.dev/flutter/widgets/State-class.html

Reference mounted State: https://api.flutter.dev/flutter/widgets/State/mounted.html

like image 164
MatPag Avatar answered Oct 04 '22 08:10

MatPag


In some cases you will need to start an animation or change the state when you create your Widget, then is not possible to do that in your constructor because your Widget is not inserted in the tree yet.

Example of AnimationController

    AnimationController _animationController ;      ...        @override         void initState() {         ... instance the animationController            _animationController.forward();           super.initState();         } 

Another example, when you receive some params from another Widget, let say your StatefulWidget has a param named title and you want to create a local variable in your State class to handle the state, you will have to do something like this:

  class ExampleWidget extends StatefulWidget {     final String title;     ExampleWidget({this.title});    ....     YourStateClass extends State<ExampleWidget> {    var localVariable;    @override         void initState() {           localVariable = widget.title;           super.initState();         } 

And now you could use your localVariable inside your widget tree to update the state.

like image 26
diegoveloper Avatar answered Oct 04 '22 07:10

diegoveloper