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/avoid-init-to-null.md. A documentation index is available at /llms.txt.

Don't explicitly initialize variables to null

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

Metadata

ID: dart-code-quality/avoid-init-to-null

Language: Dart

Severity: Notice

Category: Code Style

Description

If a variable has a non-nullable type or is final, Dart reports a compile error if you try to use it before it has been definitely initialized. If the variable is nullable and not const or final, then it is implicitly initialized to null for you. There’s no concept of “uninitialized memory” in Dart and no need to explicitly initialize a variable to null to be “safe”. Adding = null is redundant and unneeded.

Non-Compliant Code Examples

int? a = null;
String? b = null;

Item? bestDeal(List<Item> cart) {
  Item? bestItem = null;
  return bestItem;
}

Compliant Code Examples

int? a;
String? b;
bool? c;
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