use_ is_ even_ rather_ than_ modulo
Details about the 'use_is_even_rather_than_modulo' diagnostic produced by the Dart analyzer.
Use '{0}' rather than '% 2'.
Description
#
The analyzer produces this diagnostic when the result of
applying the modulo operator % to an integer value,
with 2 as the right operand, is compared to 0 or 1
using ==
rather than using the isEven or isOdd getter.
Examples
#
The following code produces this diagnostic because the result of
x % 2 is compared to 0 to check whether x is even:
bool f(int x) => x % 2 == 0;
The following code produces this diagnostic because the result of
x % 2 is compared to 1 to check whether x is odd:
bool f(int x) => x % 2 == 1;
Common fixes
#
If you're checking whether a value is even,
then use the isEven getter:
bool f(int x) => x.isEven;
If you're checking whether a value is odd,
then use the isOdd getter:
bool f(int x) => x.isOdd;
Unless stated otherwise, the documentation on this site reflects Dart 3.12.2. Report an issue.