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;
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。