Dart Interpolation
String interpolation writes a value inside a string. A dollar sign inside the string marks the spot for the value.
A variable inside a string
The string holds $name where the value belongs. Dart swaps $name for Ana when the line runs.
void main() {
var name = 'Ana';
print('Hi $name');
}Two values in one string
Each $ name in the string is swapped for its own value. The city and the year both land in place.
void main() {
var city = 'York';
var year = 2026;
print('$city in $year');
}A calculation inside braces
Braces hold a whole calculation inside the string. Dart adds 90 and 5 and writes the total 95.
void main() {
print('Score: ${90 + 5}');
}A length inside a string
The braces can hold a property of a string too. The word dart reports its own length inside the sentence.
void main() {
var word = 'dart';
print('The word $word has ${word.length} letters');
}Example
void main() {
var name = 'Ana';
var score = 88;
print('$name scored $score points');
print('Next level at ${score + 12} points');
}Key points
- A $ sign before a variable name inside a string inserts that value.
- An expression is a piece of code that works out a value.
- Braces ${...} hold an expression inside a string.
- Dart works out the inserted value when the string is built.
- One string can hold plain text and inserted values together.
In this example, the first print swaps $name for Ana and $score for 88. The second print adds 12 to score inside braces. The sum 100 lands inside the sentence.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท DartPad
Exercise
When name holds the string 'Bo', what does print('Hi $name') show?