Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between didChangeDependencies and initState?

Tags:

flutter

dart

I am new to flutter and when I want to call my context in InitState it throws an error : which is about BuildContext.inheritFromWidgetOfExactType but then I use didChangeDependencies and it works correctly.

Now I have 2 question:

1- Why calling our context in initState does not work but it works when calling from didChangeDependencies ? (because as I read in official doc This method is also called immediately after [initState], and both of them will be called before build method. )

2- Why do we have access to our context outside of build method ( because there we have build(BuildContext context) and we can use our context but in didChangeDependencies we don't have anything like didChangeDependencies(BuildContext context) , so from where can we call context to use it) ?

like image 220
mohammad Avatar asked Oct 14 '19 07:10

mohammad


People also ask

What is didChangeDependencies?

According to the Flutter official docs, didChangeDependencies() is called when a dependency of the State object changes or immediately after initState(). It is safe to call BuildContext.

What is 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() .

How many times initState is called?

The first time we see A, it's fine it only runs once. Then we go to B, and then once we return to A, initState() is called twice.

Is context available in initState?

The member variable context can be accessed during initState but can't be used for everything. This is from the flutter for initState documentation: You cannot use [BuildContext.


2 Answers

Context of a state is available to us from the moment the State loads its dependencies.

At the time build is called, context is available to us and is passed as an argument.

Now moving on, initstate is called before the state loads its dependencies and for that reason no context is available and you get an error for that if you use context in initstate. However, didChangeDependencies is called just a few moments after the state loads its dependencies and context is available at this moment so here you can use context.

However both of them are called before build is called. The only difference is that one is called before the state loads its dependencies and the other is called a few moments after the state loads its dependencies.

like image 138
daddy_ Avatar answered Sep 28 '22 22:09

daddy_


I've found a significant difference between initState and didChangeDependencies:

  • initState is called only once for a widget.
  • didChangeDependencies may be called multiple times per widget lifecycle (in my case it was called when the keyboard appears / disappears)
like image 45
fedor.belov Avatar answered Sep 29 '22 00:09

fedor.belov