no_runtimeType_toString
Avoid calling toString()
on runtimeType
.
This rule is available as of Dart 2.8.
Details
#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
Usage
#To enable the no_runtimeType_toString
rule,
add no_runtimeType_toString
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- no_runtimeType_toString
除非另有说明,文档之所提及适用于 Dart 3.6.0 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。