What is the correct order of calling superclass methods in initState, didUpdateWidget, dispose and other build methods? Why?

huangapple go评论61阅读模式
英文:

What is the correct order of calling superclass methods in initState, didUpdateWidget, dispose and other build methods? Why?

问题

在Android开发世界中,有一组Fragment/Activity生命周期超类方法调用的固定顺序。我参考了这个答案

关于Flutter,如果我在IDE(Visual Code和Android Studio都一样)中输入一个空的StatefulWidget,然后接受提示,最终会得到以下顺序,其中可以在调用之前进行实现:

  @override
  void didUpdateWidget(covariant AnimatedIconsCircle oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

超类方法调用顺序的建议与Flutter代码库相反:

  /// 实现此方法应从继承的方法开始,如 `super.didUpdateWidget(oldWidget)`。
  @mustCallSuper
  @protected
  void didUpdateWidget(covariant T oldWidget) { }

什么是正确的方法?这真的重要吗?"

英文:

In the Android development world there is a set order of Fragment/Activity lifecycle superclass method calls. I refer to the answer.

Now, regarding Flutter, if I type an empty StatefulWidget in IDE (both Visual Code and Android Studio) didUpdateWidget or any other callback, then I accept the prompt, I end up with such an order with place for implementation before the call.

  @override
  void didUpdateWidget(covariant AnimatedIconsCircle oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

The superclass method call order proposition is in contrary to the Flutter codebase:

  /// Implementations of this method should start with a call to the inherited
  /// method, as in `super.didUpdateWidget(oldWidget)`.
  @mustCallSuper
  @protected
  void didUpdateWidget(covariant T oldWidget) { }

What's the correct approach? Does it even matter?"

答案1

得分: 1

没有提供信息。我使用InheritedWidget 进行了测试,顺序无关紧要。

此方法的实现应该以调用继承的方法开始,如super.didUpdateWidget(oldWidget)。

此方法的实现应该以调用继承的方法结束,如super.dispose()。

英文:

I realised that the docs are obvious about some of the build methods:

No information. I tested it with InheritedWidget and the order doesn't matter.

huangapple
  • 本文由 发表于 2023年2月24日 03:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定