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:
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
:
void f(Iterable<int> p) => p.isEmpty ? 0 : p.first;
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.