Reputation: 557
In the documentation it is written but I am not able to understand it.
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then the State should subscribe to that object during initState, unsubscribe from the old object and subscribe to the new object when it changes in didUpdateWidget, and then unsubscribe from the object in dispose.
You cannot use BuildContext.inheritFromWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.inheritFromWidgetOfExactType can be used there.
If you override this, make sure your method starts with a call to
super.initState()
.
But I'm not sure about its meaning. Can you explain it?
Upvotes: 33
Views: 55555
Reputation: 59
thank you for the answers, but i will also reiterate what the guys above have stated
@override
initState() { // this is called when the class is initialized or called for the first time
super.initState(); // this is the material super constructor for init state to link your instance initState to the global initState context
}
Upvotes: 1
Reputation: 344
Please allow me to quote the content written by others. I think his explanation is excellent.
https://www.geeksforgeeks.org/flutter-initstate/
There are two types of widgets provided in Flutter.
- The Stateless Widget
- The Stateful Widget
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. It is basically the entry point for the Stateful Widgets. initState() method is called only and only once and is used generally for initializing the previously defined variables of the stateful widget. initState() method is overridden mostly because as mentioned earlier it is called only once in its lifetime. If you want to trigger it again you have to shift the control to an entirely new screen and a new state.
Upvotes: 1
Reputation: 268054
Credit to @Remi, initState()
is a method which 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 initialisation work like registering a listener because, unlike build()
, this method is called once.
And to unregister your listener (or doing some post work), you override dispose()
method.
From here
A subclass of State can override initState to do work that needs to happen just once. For example, override initState to configure animations or to subscribe to platform services. Implementations of initState are required to start by calling super.initState
When a state object is no longer needed, the framework calls dispose() on the state object. Override the dispose function to do cleanup work. For example, override dispose to cancel timers or to unsubscribe from platform services. Implementations of dispose typically end by calling super.dispose
Upvotes: 31
Reputation: 7973
Uses of initState()
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()
.
Subscribe to Streams
.
Upvotes: 22