What is Dart String Interpolation?
Are you trying to display dynamic information in your app without hardcoding every single value? Whether it’s showing a user’s name, an API response, or a calculated result, most applications need to mix variable data with static text.
That’s when you might want to know about string interpolation.
In Dart, string interpolation allows you to insert variables or expressions directly within string literals. It’s a concise and reliable way to generate dynamic text output, keeping your code readable and efficient.
Syntax and Basic Usage
In Dart, string interpolation is done using the $ symbol inside a string literal. You can include a variable or even an evaluated expression directly within the text, and Dart will automatically replace it with its value when the string is created.
If you’re referencing a simple variable, you can use $variable. When you need to evaluate an expression, wrap it inside ${}.
void main() {
String name = 'Luna';
int age = 3;
print('My dog $name is $age years old.');
print('Next year, she will be ${age + 1}.');
}
Output:
My dog Luna is 3 years old.
Next year, she will be 4.
Dart automatically calls the toString() method on any non-string value used inside interpolation, so you don’t need to convert integers, doubles, or booleans manually. This keeps your output clean and reduces extra code.
Embedding Expressions
String interpolation in Dart isn’t limited to simple variables. You can also include expressions, calculations, or function calls directly inside a string using ${}. Dart evaluates whatever is inside the braces and inserts the result into the final output.
void main() {
int a = 4;
int b = 6;
print('Sum: ${a + b}');
print('Square of sum: ${(a + b) * (a + b)}');
}
Output:
Sum: 10
Square of sum: 100
The braces {} are optional when you’re using a single identifier like $a, but they’re required for any expression that involves operators, properties, or method calls.
Pro Tip: Keeping expressions short inside interpolation helps maintain readability, especially when printing logs or Flutter widget text.
String Interpolation Common Mistakes
Even though Dart’s string interpolation is straightforward, a few common syntax and logic issues can cause unexpected results.
1. Missing braces in complex expressions: When accessing object properties or calling methods, braces are required.
print('$user.name'); // Tries to access variable named user.name
print('${user.name}'); // Correct
2. Using raw strings: Strings prefixed with r are treated literally, meaning interpolation doesn’t occur.
print(r'Total: $amount'); // Prints "Total: $amount"
3. Interpolating null values: If a variable is null, Dart converts it to the string “null”. Handle such cases before printing.
String? city;
print('City: $city'); // Output: City: null
4. Interpolation in const strings: Only compile-time constants can be interpolated inside const string literals.
const name = 'Luna';
const message = 'Hello $name'; // Works
final today = DateTime.now(); // const greeting = 'Today is $today'; Not allowed
Wrapping Up
String interpolation in Dart keeps your code readable and expressive by letting you embed variables and expressions directly into strings. It’s faster to write, easier to maintain, and more natural to read than traditional concatenation.
As a best practice, prefer interpolation whenever you need to combine text with dynamic data. It works in both single-line and multiline strings, and it’s best to keep expressions simple inside ${} for clarity. Interpolation in const strings is limited to compile-time values, and defining a custom toString() method in your classes ensures clean, meaningful output when printing objects.
String interpolation is one of Dart’s simplest yet most powerful features, helping you write cleaner, faster, and more reliable Flutter code.