目录

tighten_type_of_initializing_formals

Tighten type of initializing formal.

此规则自 Dart 2.12 版本起可用。

详情

#

Tighten the type of an initializing formal if a non-null assert exists. This allows the type system to catch problems rather than have them only be caught at run-time.

BAD:

dart
class A {
  A.c1(this.p) : assert(p != null);
  A.c2(this.p);
  final String? p;
}

GOOD:

dart
class A {
  A.c1(String this.p);
  A.c2(this.p);
  final String? p;
}

class B {
  String? b;
  B(this.b);
}

class C extends B {
  B(String super.b);
}

使用方法

#

要启用 tighten_type_of_initializing_formals 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 tighten_type_of_initializing_formals

analysis_options.yaml
yaml
linter:
  rules:
    - tighten_type_of_initializing_formals