prefer_initializing_formals  
Use initializing formals when possible.
此规则自 Dart 2.0 版本起可用。
_规则集:recommended , flutter _
此规则提供 快速修复 。
详情
#DO use initializing formals when possible.
Using initializing formals when possible makes your code more terse.
BAD:
class Point {
  num? x, y;
  Point(num x, num y) {
    this.x = x;
    this.y = y;
  }
}GOOD:
class Point {
  num? x, y;
  Point(num this.x, num this.y);
}BAD:
class Point {
  num? x, y;
  Point({num? x, num? y}) {
    this.x = x;
    this.y = y;
  }
}GOOD:
class Point {
  num? x, y;
  Point({required num this.x, required num this.y});
}NOTE: This rule will not generate a lint for named parameters unless the parameter name and the field name are the same. The reason for this is that resolving such a lint would require either renaming the field or renaming the parameter, and both of those actions would potentially be a breaking change. For example, the following will not generate a lint:
class Point {
  bool? isEnabled;
  Point({bool? enabled}) {
    this.isEnabled = enabled; // OK
  }
}NOTE: Also note that it is possible to enforce a type that is stricter than the initialized field with an initializing formal parameter. In the following example the unnamed Bid constructor requires a non-null int despite amount being declared nullable (int?).
class Bid {
  final int? amount;
  Bid(int this.amount);
  Bid.pass() : amount = null;
}使用方法
#要启用 prefer_initializing_formals 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 prefer_initializing_formals :
linter:
  rules:
    - prefer_initializing_formals除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.