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/unnecessary-brace-in-string-interps.md. A documentation index is available at /llms.txt.

Avoid unnecessary braces in string interpolations

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

Metadata

ID: dart-code-quality/unnecessary-brace-in-string-interps

Language: Dart

Severity: Notice

Category: Code Style

Description

When a string interpolation contains only a simple identifier, the curly braces are unnecessary. Replace ${variable} with $variable to reduce visual noise.

Braces are only needed when the interpolated expression contains dots, calls, operators, or other constructs that cannot be parsed as a bare identifier, such as ${user.name} or ${getCount()}.

Non-Compliant Code Examples

void main() {
  var name = 'Alice';
  var count = 42;
  var flag = true;
  const _tag = 'TAG';
  var a = 'Hello ${name}!';
  var b = 'Count: ${count}';
  var c = 'Flag: ${flag}';
  var d = '${_tag}: some message';
}

Compliant Code Examples

void main() {
  var name = 'Alice';
  var greeting = 'Hello $name';
  var count = 42;
  var msg = 'Count: $count items';
  var obj = Object();
  var msg1 = 'Value: ${obj.toString()}';
  var a = 1;
  var b = 2;
  var msg2 = 'Sum: ${a + b}';

  // Underscore-prefixed identifier without braces — already correct form
  const _tag = 'TAG';
  final insecureBytes = <int>[];
  print('$_tag: Insecure bytes: ${insecureBytes.toString()}');
}

class Grid {
  final int width;
  final int height;
  final int bombCount;
  Grid(this.width, this.height, this.bombCount);
  // Braces required: '$widthh' and '$heightm' would read as wrong identifiers
  String toString() => 'w${width}h${height}m$bombCount';
}
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