跳到主要内容

use_is_even_rather_than_modulo

稳定

推荐使用 intValue.isOdd/isEven 而不是检查 % 2 的结果。

详情

#

推荐 使用 intValue.isOdd/isEven 来检查是否为偶数。

错误

dart
bool isEven = 1 % 2 == 0;
bool isOdd = 13 % 2 == 1;

正确

dart
bool isEven = 1.isEven;
bool isOdd = 13.isOdd;

启用

#

要启用 use_is_even_rather_than_modulo 规则,请在您的 analysis_options.yaml 文件中的 linter > rules 下添加 use_is_even_rather_than_modulo

analysis_options.yaml
yaml
linter:
  rules:
    - use_is_even_rather_than_modulo

如果您改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 use_is_even_rather_than_modulo: true

analysis_options.yaml
yaml
linter:
  rules:
    use_is_even_rather_than_modulo: true