dart string interpolation
dart string interpolation

Hi Guy’s Welcome to Proto Coders Point.

Understanding String Interpolation in Dart

In Dart Variable values can be embedded into string literals. This using Dart String Interpolation we can eliminate technique like we used to do i.e cumbersome string concatenation(Where we use + Operator to concate string), Here with String Interpolation feature in dart provide a clearer way to display String in dart.

By using String Interpolation in dart we can embed variable value or expression inside a string. Syntax looks like this ${expression} or $variableName.

Dart String Interpolation Example 1

Below are few Example by which you get better understanding about String Interpolations

void main() {
  String name = 'Alice';
  int age = 30;

  String greeting = 'Hello, $name! You are $age years old.';
  print(greeting); // Output: Hello, Alice! You are 30 years old.

Here in above source code, You can see name & age are two variable with values in it, are embedded into s string greeting by using $name & $age Syntax.

Example 2 – Embedding Expressing Dart String Interpolation

While performing any calculation or while using expression, You need to make use of ${} i.e. curly braces.

void main() {
  int a = 5;
  int b = 3;

  String result = 'The sum of $a and $b is ${a + b}.';
  print(result); // Output: The sum of 5 and 3 is 8.

Here a + b is performed and the final string result is embedded into the string.

Example 3 – Multi-Line Strings

void main() {
  String firstName = 'John';
  String lastName = 'Doe';

  String introduction = '''
  My name is $firstName $lastName.
  I am learning Dart.

  // Output:
  // My name is John Doe.
  // I am learning Dart.

In dart by using triple quotes, we can create a multiline string & interpolate the variable into them.

Example 4 – Method & Properties access into String

In dart by using string interpolation syntax we can also include method & property access Example below.

void main() {
  DateTime currentDate= DateTime.now();

  String currentTime = 'Current time is: ${currentDate.hour}:${currentDate.minute}:${currentDate.second}';
  // Output: Current time is: HH:MM:SS (actual time values)

Here we are getting current DataTime by using DateTime.now() Class Object, It has several properties like hours, minutes & seconds that we can easily access into strings by using syntax ${currentDate.hours} , ${currentDate.minute} ${currentDate.second}.


String Interpolation in dart is the best way to access values to dynamic strings. By doing this your codes is much more readable & maintainable. Whether you are working with simple string concatenation or complex data representation String Interpolation technique is very must essential in Flutter Dart Programming.