why not always use curly braces in string interpolation?

Viewed 1564

https://dart.dev/guides/language/effective-dart/usage#avoid-using-curly-braces-in-interpolation-when-not-needed says to avoid using curly braces in interpolation when not needed. If you’re interpolating a simple identifier not immediately followed by more alphanumeric text, the {} should be omitted.

Is there a problem doing it aside from it being not necessary to do interpolation?

The guide says this is good:

'Hi, $name!'
    "Wear your wildest $decade's outfit."
    'Wear your wildest ${decade}s outfit.'

The guide says this is bad:

'Hi, ${name}!'
    "Wear your wildest ${decade}'s outfit."
1 Answers

It is not be a problem since $decade are just a shorthand for ${decade}. So both Strings will end up compiling into the same code. The linter rule is about taste and make the code consistent in your code base.

You can see this in Dart Language Specification in the chapter about "String Interpolation": https://dart.dev/guides/language/specifications/DartLangSpec-v2.2.pdf

The form $id is equivalent to the form ${id}.

To customize which linter rules will run: https://dart.dev/guides/language/analysis-options

Related