What is the difference between didChangeDependencies and initState?

后端 未结 8 939
刺人心
刺人心 2021-01-31 17:09

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 did

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 18:10

    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 u use context in initstate. However didChangeDependencies is called just 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. Only difference is that one is called before the state loads its dependencies and other is called a few moments after the state loads its dependencies.

提交回复
热议问题