简体   繁体   English

“状态”class 构造函数在 Flutter 中是什么样子的?

[英]What does the "State" class constructor look like in Flutter?

What does super.didChangeDependencies();什么是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?我知道它应该调用State class 的constructor ,但我找不到构造函数的定义,也不明白调用它的目的是什么?

From the Flutter docs for didChangeDependencies :来自didChangeDependencies的 Flutter 文档:

Called when a dependency of this State object changes.当此 State object 的依赖项更改时调用。

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.例如,如果先前的构建调用引用了一个后来更改的 InheritedWidget,则框架将调用此方法来通知此 object 有关更改。

This method is also called immediately after initState.此方法也在 initState 之后立即调用。 It is safe to call BuildContext.dependOnInheritedWidgetOfExactType from this method.从此方法调用 BuildContext.dependOnInheritedWidgetOfExactType 是安全的。

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 (eg,.network fetches) when their dependencies change, and that work would be too expensive to do for every build.一些子类确实重写了这个方法,因为当它们的依赖关系发生变化时,它们需要做一些昂贵的工作(例如,.network 获取),而对于每个构建来说,这些工作太昂贵了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM