What is the difference between didChangeDependencies and initState?

后端 未结 8 933
刺人心
刺人心 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:12

    Called when a dependency of this State object changes.

    For example, if the previous call to build referenced an InheritedWidget that later changed, the framework would call this method to notify this object about the change.

    This method is also called immediately after initState. It is safe to call BuildContext.dependOnInheritedWidgetOfExactType from this method.

    In fact Subclasses rarely override this method because the framework always calls build after a dependency changes. Some subclasses do override this method because they need to do some expensive work (e.g., network fetches) when their dependencies change, and that work would be too expensive to do for every build.

提交回复
热议问题