---
title: "Building Strings in Dart with Interpolation, Raw Literals and Line Breaks"
date: "2026-12-03T09:55:00+07:00"
description: "Master Dart strings: $interpolation, ${expressions}, adjacent literals, triple-quoted blocks and r-prefixed raw strings for regexes and Windows paths."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-string-interpolation-raw-strings.jpg"
alt: "Conceptual illustration of Dart string building with interpolation braces, triple quotes and a raw string prefix"
tags: ['dart', 'dart-language', 'strings', 'string-interpolation', 'syntax']
---

A Dart string is more than a quoted blob of text: it can embed expressions, wrap across lines, span paragraphs, and switch off escape processing entirely with a single `r`. Knowing which of the four literal forms to reach for is the difference between code that reads like prose and code that reads like noise.

## Choosing your quotes

Dart strings can be written with single or double quotes, and the two forms are completely equivalent. The practical rule is to pick whichever one lets you avoid escaping the delimiter the text already contains.

```dart
void main() {
  var s1 = 'Single quotes work well for string literals.';
  var s2 = "Double quotes work just as well.";
  var s3 = 'It\'s easy to escape the string delimiter.';
  var s4 = "It's even easier to use the other delimiter.";
  print('$s1 / $s2');
  print('$s3 / $s4');
}
```

`==` compares strings by their sequence of code units, so two separately built literals with the same content are equal — even though, unless both are `const`, they are distinct objects. Equality of content versus identity of object is the same distinction drawn between `final` and `const` in `dart-final-vs-const.md`.

## Interpolation: $identifier and ${expression}

Putting a value inside a string uses `$` followed by an identifier, or `${` followed by any expression. If the value is a simple identifier, the braces are optional; for anything else — a method call, an arithmetic expression, an identifier immediately followed by more letters — the braces are required.

```dart
void main() {
  var name = 'string interpolation';
  var decade = 1970;
  print('Dart has $name, which is very handy.');
  print('That deserves all caps. ${name.toUpperCase()} is very handy!');
  print('I love your ${decade}s costume.');
}
```

That last line is the classic trap: write `$decades` instead of `${decade}s` and the compiler looks for an identifier named `decades`, which does not exist. The same applies whenever the interpolation is immediately followed by a letter or underscore. Under the hood, interpolation calls the value's `toString()`, which is why it works on any object, not just strings.

## Prefer interpolation over the + operator

Effective Dart's `prefer_interpolation_to_compose_strings` rule exists because chains of `+` are almost always louder and more error-prone than the equivalent interpolation, especially once `.toString()` calls start appearing.

```dart
void main() {
  var name = 'Ada';
  var year = 2026;
  var birth = 1815;
  // Preferred: interpolation.
  print('Hello, $name! You are ${year - birth} years old.');
  // Works, but noisy: concatenation with +.
  print('Hello, ' +
      name +
      '! You are ' +
      (year - birth).toString() +
      ' years old.');
}
```

Both lines print the same sentence. The rule does allow `.toString()` when you are converting a single object with nothing else in the string — it is the chains of `+` that the lint targets.

## Line breaks with adjacent string literals

The `prefer_adjacent_string_concatenation` rule says the opposite of the `+` habit: when you are joining string *literals* — not values — just place them next to each other. Adjacent literals are glued at compile time, which is the idiomatic way to keep a long string inside your line-length limit.

```dart
void main() {
  var s1 =
      'String '
      'concatenation'
      ' works even over line breaks.';
  var warning = 'ERROR: Parts of the spaceship are on fire. Other '
      'parts are overrun by martians. Unclear which are which.';

  print(s1);
  print(warning);
}
```

The quotes may even differ between adjacent literals; the compiler only requires that they are literals sitting side by side. Mixing the two techniques is common: a long message split across adjacent literals with `$values` interpolated inside each piece.

## Triple quotes for multi-line strings

When the content itself spans lines — SQL, templates, log samples — triple quotes let you write the text verbatim instead of stitching it together.

```dart
void main() {
  var s1 = '''You can create
multi-line strings like this one.''';
  var s2 = """This is also a
multi-line string.""";
  print(s1);
  print(s2);
}
```

Both `'''` and `"""` work, and escapes such as `\n` are still processed inside them — a triple-quoted string is ordinary, not raw. The choice between the two usually comes down to whether the content contains apostrophes or double quotes more often.

## Raw strings: turn escape processing off

Prefix any string with `r` and backslashes lose all special meaning. This is indispensable for regular expressions, Windows paths, and LaTeX-ish text where every backslash is meant to be a literal character.

```dart
void main() {
  var path = r'C:\Users\dart\projects';
  var pattern = r'\d+';
  var match = RegExp(pattern).firstMatch('order 42 shipped');
  print(path);
  print(match?.group(0)); // 42
}
```

Without the `r`, the backslashes in those strings would start escape sequences — `\n` would become a newline, `\d` is not a valid escape at all — so the pattern would be wrong or fail to compile. Raw strings are still ordinary strings in every other respect: they interpolate nothing (there is no `$` processing either) and they compare normally.

## Const strings and their limits

String literals are compile-time constants, and a `const` string may interpolate — provided every interpolated value is itself a compile-time constant evaluating to null, a number, a string or a bool.

```dart
void main() {
  const aConstNum = 0;
  const aConstBool = true;
  const aConstString = 'a constant string';
  const validConstString = '$aConstNum $aConstBool $aConstString';
  print(validConstString);
}
```

Interpolate anything runtime — a `var`, a list, a function call — inside a `const` declaration and the compiler rejects it. In practice that means constants compose freely with other constants (a nice fit with the `const` guidance in `dart-final-vs-const.md`), while everything else stays a plain runtime string.

## The literal forms at a glance

| Form | Example | Reach for it when |
|---|---|---|
| Single quotes | `'hi'` | The default; shortest when text has no apostrophes |
| Double quotes | `"It's here"` | The text contains the other delimiter |
| Interpolated | `'Hi, $name!'` | Embedding values; `${expr}` for anything complex |
| Adjacent literals | `'one ' 'two'` | Splitting one long literal across lines |
| Triple-quoted | `'''line one\nline two'''` | Content that genuinely spans lines |
| Raw (`r` prefix) | `r'\d+'` | Regexes, Windows paths — backslashes stay literal |
| Const string | `const 'v1'` | Compile-time configuration composed of constants |

## Takeaways

Dart gives you four literal shapes rather than one: ordinary quoted strings, interpolated ones with `$` or `${}`, adjacent and triple-quoted forms for line breaks and blocks, and `r`-prefixed raw strings that suspend escape processing. Effective Dart's three string rules — prefer interpolation over `+`, drop unnecessary braces, and join literals by placing them adjacent — all push the same direction: let the literals do the work so the values stand out.

**Takeaway:** Use `$identifier` for simple interpolation, `${expression}` when braces are genuinely needed, adjacent and triple-quoted literals for line breaks, and an `r` prefix the moment backslashes must stay literal.

## References

- https://dart.dev/language/built-in-types
- https://dart.dev/effective-dart/usage
- https://dart.dev/language
