prefer_is_empty
Use isEmpty
for Iterable
s and Map
s.
This rule is available as of Dart 2.0.
Rule sets: core, recommended, flutter
This rule has a quick fix available.
Details
#DON'T use length
to see if a collection is empty.
The Iterable
contract does not require that a collection know its length or be
able to provide it in constant time. Calling length
just to see if the
collection contains anything can be painfully slow.
Instead, there are faster and more readable getters: isEmpty
and
isNotEmpty
. Use the one that doesn't require you to negate the result.
BAD:
if (lunchBox.length == 0) return 'so hungry...';
if (words.length != 0) return words.join(' ');
GOOD:
if (lunchBox.isEmpty) return 'so hungry...';
if (words.isNotEmpty) return words.join(' ');
Usage
#To enable the prefer_is_empty
rule,
add prefer_is_empty
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- prefer_is_empty
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。