英文:
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
- 我意识到文档明确说明了一些构建方法:
此方法的实现应该以调用继承的方法开始,如super.initState()。
没有提供信息。我使用InheritedWidget
进行了测试,顺序无关紧要。
此方法的实现应该以调用继承的方法开始,如super.didUpdateWidget(oldWidget)。
此方法的实现应该以调用继承的方法结束,如super.dispose()。
英文:
I realised that the docs are obvious about some of the build methods:
- https://api.flutter.dev/flutter/widgets/State/initState.html
> Implementations of this method should start with a call to the inherited method, as in super.initState(). - https://api.flutter.dev/flutter/widgets/State/didChangeDependencies.html
No information. I tested it with InheritedWidget
and the order doesn't matter.
- https://api.flutter.dev/flutter/widgets/State/didUpdateWidget.html
> Implementations of this method should start with a call to the inherited method, as in super.didUpdateWidget(oldWidget). - https://api.flutter.dev/flutter/widgets/State/dispose.html
> Implementations of this method should end with a call to the inherited method, as in super.dispose().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论