目录

no_runtimeType_toString

Avoid calling toString() on runtimeType.

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

详情

#

Calling toString on a runtime type is a non-trivial operation that can negatively impact performance. It's better to avoid it.

BAD:

dart
class A {
  String toString() => '$runtimeType()';
}

GOOD:

dart
class A {
  String toString() => 'A()';
}

This lint has some exceptions where performance is not a problem or where real type information is more important than performance:

  • in an assertion
  • in a throw expression
  • in a catch clause
  • in a mixin declaration
  • in an abstract class declaration

使用方法

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - no_runtimeType_toString