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
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
// 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';
}String examples(String name) { final a = '$name'; final a = '$name'; final b = '${name}'; final b = '${name}'; final c = "$name"; final s = 'x'; final d = '$s'; final d = '$s'; final e = '${'lit'}'; final f = 'a' + 'b'; final g = '$f'; final g = '$f'; final h = '${name.toUpperCase()}';Valid
// 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.
{
"linter": {
"rules": {
"style": {
"unnecessary-string-interpolations": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-string-interpolations: <reason>More style rules
- unnecessary-this
unnecessary_thisrecommendedFlags a redundant
this.qualifier on member access. - unreachable-from-main
unreachable_from_mainReport public declarations that no entry point of an executable library reaches.
- use-colored-box
use_colored_boxrecommendedflutterPrefers
ColoredBoxover aContainerthat only paints a color behind its child. - use-decorated-box
use_decorated_boxrecommendedflutterPrefers
DecoratedBoxover aContainerthat only decorates its child. - use-design-system-item
use-design-system-itemflutterFlags construction of a widget or type that should be replaced by its design-system equivalent.
- use-enums
use_enumsFlags 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.