
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.