0

What does super.didChangeDependencies(); do in the following code?

  void didChangeDependencies() {
    if (_isInit) {
      setState(() {
        _isLoading = true;
      });
      Provider.of<Contests>(context).fetchAndSetContests().then((_) {
        setState(() {
          _isLoading = false;
        });
      });
    }
    _isInit = false;
    super.didChangeDependencies();
  }

I know it should call the constructor of the State class but I can't find the constructor's definition and don't understand what is the purpose of calling that?

best_of_man
  • 643
  • 2
  • 15
  • 1
    If you select the State class name in your code in any proper IDE, you should have a right-click menu option to "go to definition". That will take you to the source code of State. On the mac on VSC, I can also command-click on the name, but that may not be universal. – Randal Schwartz Jan 22 '23 at 03:31
  • @RandalSchwartz: I am looking for `State` class's constructor but still can not find it. – best_of_man Jan 23 '23 at 02:43
  • That's right where I said it would be. You can also try "go to symbol". For me on the mac with VSC, that's command-T. And type "State". Should take you right to the definition. – Randal Schwartz Jan 23 '23 at 08:35
  • 1
    You can view it in the source code at https://github.com/flutter/flutter/blob/135454af32477f815a7525073027a3ff9eff1bfd/packages/flutter/lib/src/widgets/framework.dart#LL899 – Randal Schwartz Jan 23 '23 at 08:39

1 Answers1

0

From the Flutter docs for didChangeDependencies:

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.

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.

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61