close

flutter get context in initstate method

Along these lines, a great deal of new to Flutter engineers are confronting issues understanding initState() and didChangeDependencies() strategy, Though Flutter has got it canvassed in authentic docs yet beginner designers are thinking that its difficult to get them.

initState()

As per Flutter official docs,Called when this item is embedded into the tree.

The structure will call this strategy precisely once for each State object it makes.

Supersede this technique to perform introduction that relies upon the area at which this article was embedded into the tree (i.e., setting) or on the gadget used to arrange this item (i.e., gadget).

EXPLAINATION:-

At whatever point we begin making a gadget i.e a class and begin fabricating the layers or tree of gadgets utilizing Build Context(), we can instate any factor, call any capacity or technique prior to building the gadget tree i.e prior to building the gadget anything inside initState() capacity will be called or introduced first and the gadgets are constructed later. Allow me to clarify utilizing a straightforward model:

Image for post
Use of initState()

As should be obvious in the code above, prior to building the gadget tree initState() is introduced and called where print explanation is utilized to print ‘open’, you can utilize initState() to set qualities to any variable,list or even use it to call any capacity.

didChangeDependencies()

As per Flutter official docs,Called when a reliance of this State object changes.

For instance, if the past call to construct referred to an InheritedWidget that later changed, the system would call this strategy to advise this item about the change.

This strategy is additionally called following initState. It is protected to call BuildContext.dependOnInheritedWidgetOfExactType from this technique.

Subclasses seldom supersede this strategy on the grounds that the system consistently calls work after a reliance changes. A few subclasses do supersede this technique since they need to accomplish some costly work (e.g., network brings) when their conditions change, and that work would be too costly to even think about doing for each form.

EXPLAINATION:-

Considered when a reliance of this State object changes.

For instance, if the past call to assemble referred to an InheritedWidget that later changed, the structure would call this strategy to inform this item about the change.

This strategy is likewise called following initState. It is protected to call BuildContext.dependOnInheritedWidgetOfExactType from this strategy.

its called before work after an InheritedWidget informs you that it refreshed

its valuable since it offers you a chance to think about and set state outside of assemble.

Lets take a model:

Theme.of(context) for instance is stick for dependOnInheritedWidgetOfExactType,when that InheritedWidget refreshes for example from the subject transforming, it will revamp your gadget.

Why is it useful?

its valuable since it offers you a chance to think about and set state outside of assemble.

void didChangeDependencies() {
final greatplaces= Provider.of<GreatPlaces>(context); if(this.greatPlaces!=greatplaces)
{ this.greatPlaces=greatplaces;
getList= greatplaces.fetchAndSetPlaces();}
super.didChangeDependencies();
}

a conceivable condition for didChangeDependencies in the above bit to be called here would be that your GreatPlaces supplier refreshed.

Expectation you folks comprehended the contrast between the two and you can mess with both the strategies to get a hang of it. Have a decent day you folks and see you all in the following article!

Thanks for reading this article ❤
If I got something wrong, Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.

Leave a Comment