avoid_ double_ and_ int_ checks
Learn about the avoid_double_and_int_checks linter rule.
Avoid double and int checks.
Details
#
AVOID to check if type is
double or int.
When compiled to JS, integer values are represented as floats.
That can lead to some unexpected behavior when using either
is or is! where the type is either
int or double.
BAD:
f(num x) {
if (x is double) {
...
} else if (x is int) {
...
}
}
GOOD:
f(dynamic x) {
if (x is num) {
...
} else {
...
}
}
Enable
#
To enable the avoid_double_and_int_checks rule,
add avoid_double_and_int_checks
under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- avoid_double_and_int_checks
If you're instead using the YAML map syntax to configure
linter rules, add
avoid_double_and_int_checks: true under
linter > rules:
linter:
rules:
avoid_double_and_int_checks: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.