{

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


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 ({}).

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 $ 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.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example