What is the difference between didChangeDependencies and initState?

后端 未结 8 936
刺人心
刺人心 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 17:54

    1. According to initState documentation

    You cannot use BuildContext.inheritFromWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.inheritFromWidgetOfExactType can be used there.

    So you need to use BuildContext.inheritFromWidgetOfExactType in didChangeDependencies.

    1. Every Widget has its own context. That is why you have access to context outside build method.

    Regarding build(BuildContext context), build method accepts context from the parent widget. It means this parameter BuildContext context is not current widget's context but its parent's context.

提交回复
热议问题