目錄
目錄

Avoid calling toString() on runtimeType.

This rule is available as of Dart 2.8.1.

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 assertion
  • in throw expressions
  • in catch clauses
  • in mixin declaration
  • in abstract class

Usage

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