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/recursive-getters.md. A documentation index is available at /llms.txt.

Avoid getters that recursively return themselves

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

Metadata

ID: dart-code-quality/recursive-getters

Language: Dart

Severity: Warning

Category: Code Style

Description

A getter whose body only returns itself recurses forever and throws a stack overflow at runtime, for example int get value => value;. This almost always means a backing field was forgotten. Return the backing field or a computed value instead of the getter itself.

Non-Compliant Code Examples

class Broken {
  int get value => value;

  String get name {
    return name;
  }

  int get a {
  return this.a;
  }

  int get total => this.total;
}

int get count => count;

Compliant Code Examples

class Example {
  int _value = 0;
  int a = 1;
  int b = 2;

  int get value => _value;

  int get total => a + b;

  String get label {
    return _label;
  }

  final String _label = "x";
}

int get answer => 42;
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