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.
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.
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.
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() .
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 .
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.
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.
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.
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.
The difference is (in the context of creating a State
object) which has the initState()
method:
constructor
simply create a newState
instance
initState()
is called after the object is created and at this point you have access to theBuildContext
or theStatefulWidget
to which theState
is attached to, respectively using thecontext
and thewidget
properties. At this point theState
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With