no_ runtimeType_ toString
Avoid calling toString()
on runtimeType
.
Details
#
Calling
toString
on a runtime type is a non-trivial operation that can
negatively impact performance. It's better to avoid it.
BAD:
class A {
String toString() => '$runtimeType()';
}
GOOD:
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
Enable
#
To enable the
no_runtimeType_toString
rule, add
no_runtimeType_toString
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- no_runtimeType_toString
If you're instead using the YAML map syntax to configure linter rules,
add
no_runtimeType_toString: true
under
linter > rules:
linter:
rules:
no_runtimeType_toString: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.