avoid_double_and_int_checks
避免 double
和 int
檢查。
此規則在 Dart 2.0 及更高版本中可用。
詳細資訊
#避免檢查類型是否為 double
或 int
。
編譯為 JS 時,整數值會表示為浮點數。當使用 is
或 is!
且類型為 int
或 double
時,可能會導致一些意想不到的行為。
錯誤
dart
f(num x) {
if (x is double) {
...
} else if (x is int) {
...
}
}
正確
dart
f(dynamic x) {
if (x is num) {
...
} else {
...
}
}
用法
#若要啟用 avoid_double_and_int_checks
規則,請在 analysis_options.yaml
檔案的 linter > rules 下新增 avoid_double_and_int_checks
analysis_options.yaml
yaml
linter:
rules:
- avoid_double_and_int_checks
除非另有說明,否則本網站上的文件反映 Dart 3.6.0。頁面最後更新於 2024-07-03。 檢視原始碼 或 回報問題。