{

How to print dollar symbol in string dart?| Flutter By Example


print dollar symbol in string  in Dart

You can append any string with another string using the plus operator or interpolation syntax.

Similarly, You can add a dollar symbol to a string in multiple ways.

How to print the dollar symbol in Dart and Flutter?

There are multiple ways The string contains normal text or raw text which is interpreted with interpolation syntax.

interpolation syntax uses the $ symbol to print the variable value. For example, if you interpolate syntax, You can use $price to print the variable value. if we append $ to an interpolation variable, It throws an error Error: A ‘$’ has a special meaning inside a string, and must be followed by an identifier or an expression in curly braces ({}).

The below code throws an error.

void main() {
  int price = 95;

  print("Current price is $$price");
}

One way is you need to escape the $ symbol as \$

void main() {
  int price = 95;

  print("Current price is \$$price");
}

Another way using the raw string that starts with r

void main() {
  int price = 95;

  print(r"Current price is $price");
}

Conclusion

Learned Since raw strings have a special meaning for the $ dollar symbol. To print a dollar symbol, We need to escape it.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.