Problem Statement
When is didChangeDependencies() called?
Explanation
DidChangeDependencies() is called immediately after initState() and again whenever an InheritedWidget that this widget depends on changes. For example, if you call Theme.of(context), MediaQuery.of(context), or Provider.of(context), your widget depends on those InheritedWidgets, and didChangeDependencies() will be called when they change.
This is the right place to perform initialization that depends on InheritedWidgets or BuildContext, since these dependencies aren't available in initState(). You can safely access context-dependent data here. However, be careful with expensive operations in didChangeDependencies() as it can be called multiple times.
Common uses include initializing resources based on theme data, responding to MediaQuery changes (like screen rotation), or subscribing to providers. Unlike initState() which runs once, didChangeDependencies() can run multiple times, so implement it idempotently or use flags to avoid repeating expensive initialization unnecessarily.
