no_runtimeType_toString
避免對 runtimeType 呼叫 toString()。
詳情
#對執行時型別呼叫 toString 是一項不平凡的操作,可能對效能產生負面影響。最好避免使用它。
差
dart
class A {
String toString() => '$runtimeType()';
}好
dart
class A {
String toString() => 'A()';
}此 lint 在某些情況下存在例外,此時效能不是問題,或者真實型別資訊比效能更重要
- 在斷言中
- 在 throw 表示式中
- 在 catch 子句中
- 在 mixin 宣告中
- 在抽象類宣告中
啟用
#要啟用 no_runtimeType_toString 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 no_runtimeType_toString
analysis_options.yaml
yaml
linter:
rules:
- no_runtimeType_toString如果您使用的是 YAML 對映語法來配置 linter 規則,請在 linter > rules 下新增 no_runtimeType_toString: true
analysis_options.yaml
yaml
linter:
rules:
no_runtimeType_toString: true