US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No 405, Kabir Shilp, Opp. Kansar Hotel, Opp. Landmark, Kudasan, Gandhinagar, Gujarat 382421

[email protected]

What is the Difference Between didChangeDependencies and initState?

What is the Difference Between didChangeDependencies and initState

What is the Difference Between didChangeDependencies and initState?

Earlier we have been through the flutter life cycle where we learned about the complete lifecycle of a flutter mobile application. So in this article, we will be through what is the difference between didChangeDependencies and initState.

What is the Difference Between didChangeDependencies and initState?

The context of a state is available to us from the moment the State loads its dependencies. The 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. Along with context is available at this moment so here you can use context.

  • 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.

  • Every widget has its own context. That is why you have access to a context outside the build method.

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

  • initState is called only once for every widget.
  • didChangeDependencies may be called multiple times per widget lifecycle in my case it was called when the keyboard appears/disappears
  • initState() Called when a new Widget is inserted into the tree. The framework will call this method exactly once for each [State] object it creates. This will be called once. So perform work which required to be performed only once, but remember context can’t be used here, as widget state gets loaded only initState() work is done.

Syntax:

@override
  void initState() {
    debugPrint('initState()');
    super.initState();
  }
  • didChangeDependencies() Called when a dependency of this [State] object changes.

Conclusion:

Thanks for being with us on a Flutter Journey!!!

So today, We learned about the difference between didChangeDependencies and initState.

Kindly drop us your feedback to serve you better.

Do let us know if you need any assistance with Flutter Development!!

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Post a Comment