super_goes_last
Place the super
call last in a constructor initialization list.
此规则已从最新的 Dart 版本中移除。
详情
#NOTE: This rule is removed in Dart 3.0.0; it is no longer functional.
DO place the super
call last in a constructor initialization list.
Field initializers are evaluated in the order that they appear in the constructor initialization list. If you place a super()
call in the middle of an initializer list, the superclass's initializers will be evaluated right then before evaluating the rest of the subclass's initializers.
What it doesn't mean is that the superclass's constructor body will be executed then. That always happens after all initializers are run regardless of where super
appears. It's vanishingly rare that the order of initializers matters, so the placement of super
in the list almost never matters either.
Getting in the habit of placing it last improves consistency, visually reinforces when the superclass's constructor body is run, and may help performance.
BAD:
View(Style style, List children)
: super(style),
_children = children {
GOOD:
View(Style style, List children)
: _children = children,
super(style) {
使用方法
#要启用 super_goes_last
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 super_goes_last
:
linter:
rules:
- super_goes_last
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.