{

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

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example