avoid_type_to_string
避免
此規則自 Dart 2.12 起可用。
詳細資訊
#應該避免在正式程式碼中呼叫
錯誤
dart
void bar(Object other) {
if (other.runtimeType.toString() == 'Bar') {
doThing();
}
}
Object baz(Thing myThing) {
return getThingFromDatabase(key: myThing.runtimeType.toString());
}
正確
dart
void bar(Object other) {
if (other is Bar) {
doThing();
}
}
class Thing {
String get thingTypeKey => ...
}
Object baz(Thing myThing) {
return getThingFromDatabase(key: myThing.thingTypeKey);
}
用法
#若要啟用 avoid_type_to_string
規則,請在 analysis_options.yaml
檔案中的 linter > rules 下新增 avoid_type_to_string
analysis_options.yaml
yaml
linter:
rules:
- avoid_type_to_string
除非另有說明,否則本網站的文件反映的是 Dart 3.6.0。頁面最後更新於 2024-07-03。 檢視原始碼或回報問題。