Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is initState and super.initState in flutter?

Tags:

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?

like image 662
Rajesh Shaw Avatar asked Sep 12 '18 13:09

Rajesh Shaw


People also ask

What does Super initState in flutter?

initState In 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.

When should I call super initState?

super. initState() should always be the first line in your initState method. From docs: initState(): If you override this, make sure your method starts with a call to super.

Is initState called before build?

initState() This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must also call super.

Does set state call initState?

Because we can't call setState() directly in the initState method, we can't call it from a function called by initState either. Unless this function is asynchronous and the setState call is made after the first await.


Video Answer


2 Answers

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

like image 133
CopsOnRoad Avatar answered Oct 02 '22 13:10

CopsOnRoad


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.

like image 25
Shubhamhackz Avatar answered Oct 02 '22 13:10

Shubhamhackz