prefer_is_empty
The comparison is always 'false' because the length is always greater than or equal to 0.
The comparison is always 'true' because the length is always greater than or equal to 0.
Use 'isEmpty' instead of 'length' to test whether the collection is empty.
Use 'isNotEmpty' instead of 'length' to test whether the collection is empty.
Description
#The analyzer produces this diagnostic when the result of invoking either
Iterable.length
or Map.length
is compared for equality with zero
(0
).
Example
#The following code produces this diagnostic because the result of invoking
length
is checked for equality with zero:
dart
int f(Iterable<int> p) => p.length == 0 ? 0 : p.first;
Common fixes
#Replace the use of length
with a use of either isEmpty
or
isNotEmpty
:
dart
void f(Iterable<int> p) => p.isEmpty ? 0 : p.first;