prefer_is_empty

Group: style

Maturity: stable

Dart SDK: >= 2.0.0 • (Linter v0.1.30)

Since info is static, may be stale
pedantic

View all Lint Rules

Using the Linter

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.

GOOD:

if (lunchBox.isEmpty) return 'so hungry...';
if (words.isNotEmpty) return words.join(' ');

BAD:

if (lunchBox.length == 0) return 'so hungry...';
if (words.length != 0) return words.join(' ');