目錄

avoid_double_and_int_checks

避免 doubleint 檢查。

此規則在 Dart 2.0 及更高版本中可用。

詳細資訊

#

避免檢查類型是否為 doubleint

編譯為 JS 時,整數值會表示為浮點數。當使用 isis! 且類型為 intdouble 時,可能會導致一些意想不到的行為。

錯誤

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