unnecessary-string-interpolations

Same as unnecessary_string_interpolations in Dart lints

Group
Style
Path
lint/style/unnecessary-string-interpolations
Default
On (recommended)
Origin
Dart lints
Rule
474 of 512
recommended

Flags a string literal whose entire content is a single interpolation of a provably non-nullable String.

When a string is nothing but '$x' or '${x}' and x is already a string, wrapping the value in quotes and interpolation markers is redundant — the literal is equivalent to writing the expression directly. The quotes only add noise and obscure that no formatting or concatenation is happening. The rule requires the interpolation to span the whole content, whether a bare $identifier or a full ${...} expression; a string with any surrounding text, or two adjacent interpolations like '$a$b', is left alone. Adjacent literal segments and raw literals are never flagged. Use the interpolated expression on its own.

Canonical receiver and signature facts must prove a non-nullable String. Nullable or unknown values and null-aware accesses are left alone.

Invalid

example.dartdart
// Bad: the whole string is a single interpolation of a provably non-nullable String.
String examples(String name) {
  final a = '$name';
  final b = '${name}';
  final c = "$name";
  final s = 'x';
  final d = '$s';
  final e = '${'lit'}';
  final f = 'a' + 'b';
  final g = '$f';
  final h = '${name.toUpperCase()}';
  return '$a$b$c$d$e$g$h';
}
Use the expression directly instead of this unnecessary string interpolation.
2String examples(String name) {
3 final a = '$name';
Use the expression directly instead of this unnecessary string interpolation.
3 final a = '$name';
4 final b = '${name}';
Use the expression directly instead of this unnecessary string interpolation.
4 final b = '${name}';
5 final c = "$name";
Use the expression directly instead of this unnecessary string interpolation.
6 final s = 'x';
7 final d = '$s';
Use the expression directly instead of this unnecessary string interpolation.
7 final d = '$s';
8 final e = '${'lit'}';
Use the expression directly instead of this unnecessary string interpolation.
9 final f = 'a' + 'b';
10 final g = '$f';
Use the expression directly instead of this unnecessary string interpolation.
10 final g = '$f';
11 final h = '${name.toUpperCase()}';

Valid

example.dartdart
// Good: not a single bare interpolation, or the interpolated type is not a provable String.
String examples(String name, Object value, int n, String? maybe) {
  final a = 'Hello $name'; // has surrounding text
  final b = '$name!'; // trailing text
  final c = '${name}s'; // trailing text
  final d = '$name$name'; // two interpolations
  final e = 'count: ${n}'; // leading text
  final f = 'plain'; // no interpolation
  final g = r'$name'; // raw string — literal `$name`, not an interpolation
  final h = '${n + 1}'; // int expression — the interpolation is what makes it a String
  final i = '${value}'; // Object — the interpolation is what makes it a String
  final k = '$maybe'; // String? — nullable, must not fire
  final l = '$value'; // Object interpolation
  return '$a$b$c$d$e$f$g$h$i$k$l';
}

class ShadowedField {
  String? x;

  // A try-body local must not leak past its block: `$x` below resolves to the
  // nullable field, so proving it String via the leaked local would be wrong.
  void f() {
    try {
      var x = 'lit';
      print(x);
    } catch (_) {}
    print('$x');
  }

  // Same for a finally-body local.
  void g() {
    try {
      print('');
    } finally {
      var x = 'lit';
      print(x);
    }
    print('$x');
  }
}

Configure unnecessary-string-interpolations

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-string-interpolations": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/unnecessary-string-interpolations: <reason>
  • unnecessary-thisunnecessary_thisrecommended

    Flags a redundant this. qualifier on member access.

  • unreachable-from-mainunreachable_from_main

    Report public declarations that no entry point of an executable library reaches.

  • use-colored-boxuse_colored_boxrecommendedflutter

    Prefers ColoredBox over a Container that only paints a color behind its child.

  • use-decorated-boxuse_decorated_boxrecommendedflutter

    Prefers DecoratedBox over a Container that only decorates its child.

  • use-design-system-itemuse-design-system-itemflutter

    Flags construction of a widget or type that should be replaced by its design-system equivalent.

  • use-enumsuse_enums

    Flags classes that emulate an enum and could be declared as one.

  • use-existing-destructuring

    Reuse or extend an existing record destructuring instead of rereading a field.

  • use-existing-variable

    Reuse a local for stable SDK arithmetic and String.length/int parity getters.