Given two string literals, Dart allows concatenation:
- via the
+
operator over strings, e.g. 'Hello' + 'World'
- via adjacent string literals, e.g.
'Hello' 'World'
The +
operator concatenates any two expressions of type String
, irrespective of whether the two expressions are literals
or not. This means that all the following expressions are valid:
-
'a literal' + 'another literal'
-
'a literal' + aVariable
-
aVariable + 'a literal'
On the other hand, adjacent string literals are a specific form of concatenation that only works with string literals and interpolated strings,
which means that only the first two of the following expressions are valid:
-
'a literal' 'another literal'
-
'a literal' 'an interpolated ${"string"}'
-
'a literal' aVariable
-
aVariable 'a literal'
Because concatenation of strings with variable elements should be done via string
interpolation (as encouraged by S3512), consistently using adjacent string literals for concatenation of string without the variable
elements (i.e. literals only) makes the code more readable and homogeneous, since a single syntax is used for all concatenations.