Matthew Kooshad
Matthew Kooshad

Reputation: 319

why not always use curly braces in string interpolation?

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."

Upvotes: 6

Views: 2840

Answers (1)

julemand101
julemand101

Reputation: 31249

It is not 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, see the following link: https://dart.dev/guides/language/analysis-options

Upvotes: 4

Related Questions