Strings

Domains: Dart

DO

DO use adjacent strings to concatenate string literals.

If you have two string literals—not values, but the actual quoted literal form—you do not need to use + to concatenate them. Just like in C and C++, simply placing them next to each other does it. This is a good way to make a single long string that doesn’t fit on one line.

raiseAlarm(
    'ERROR: Parts of the spaceship are on fire. Other '
    'parts are overrun by martians. Unclear which are which.');
raiseAlarm('ERROR: Parts of the spaceship are on fire. Other ' +
    'parts are overrun by martians. Unclear which are which.');

PREFER

PREFER using interpolation to compose strings and values.

If you’re coming from other languages, you’re used to using long chains of + to build a string out of literals and other values. That does work in Dart, but it’s almost always cleaner and shorter to use interpolation:

'Hello, $name! You are ${year - birth} years old.';
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';

AVOID

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.

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

Similar pages

Page structure
Terms

String

Dart