For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/dart-code-quality/prefer-is-empty.md. A documentation index is available at /llms.txt.

Use isEmpty instead of comparing length to zero

This product is not supported for your selected Datadog site. ().

Metadata

ID: dart-code-quality/prefer-is-empty

Language: Dart

Severity: Notice

Category: Code Style

Description

Comparing a collection’s .length to zero (e.g. list.length == 0) is less readable and may be slower than calling .isEmpty or .isNotEmpty directly. Use .isEmpty when checking for zero length and .isNotEmpty when checking for non-zero length.

Non-Compliant Code Examples

void main() {
  var list = [];
  if (list.length == 0) {}
  if (list.length != 0) {}
  if (list.length > 0) {}
  if (0 == list.length) {}
  if (0 != list.length) {}
}

Compliant Code Examples

void main() {
  var list = [];
  if (list.isEmpty) {}
  if (list.isNotEmpty) {}
  if (list.length == 1) {}
  if (list.length > 1) {}
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security