user11065582
user11065582

Reputation:

How to add dollar sign $ to strings in Dart?

I need to use this String value. However, I cannot seem use the dollar sign "$".

Text('! @ # $ & * ~', style: TextStyle(color: Colors.grey))

Upvotes: 17

Views: 30874

Answers (9)

Mohamed Saber
Mohamed Saber

Reputation: 1

try this: Icon(Icons.attach_money)

Upvotes: 0

Shashank Sahai
Shashank Sahai

Reputation: 107

The dollar is a special symbol and we use it for using variables inside the quotation marks.

String currency="ruppee"; 
Text('Indian currency is ${currency}');

so if we want to save the dollar symbol in string type we need to add / like this

String symbol = "\$";

Now we can use it as a string.

Upvotes: 4

Edward Edberg
Edward Edberg

Reputation: 611

If you have an amount inside a variable and you want to display it with the dollar sign, do this.

void main() {
  double amount = 49.99;
  print('\$ ${amount}');
}

Output: $ 49.99

Upvotes: 2

FelixAVeras
FelixAVeras

Reputation: 1374

Just put \$ and then works.

Text('\$800') = $800

Upvotes: 9

simply WriteText("\$200",style:TextStyle(fontSize:))

Upvotes: 4

Jewel Rana
Jewel Rana

Reputation: 2786

check below code for $ sign

`String dollarSign = String.fromCharCodes(new Runes('\u0024'));
print(dollarSign):`

Upvotes: 0

Randal Schwartz
Randal Schwartz

Reputation: 44056

Besides using a backslash in front of the dollar sign, you can also use a "raw string":

r'! @ # $ & * ~'

The "r" prefix indicates that dollar is no longer a special character.

Upvotes: 18

Sami Kanafani
Sami Kanafani

Reputation: 15741

Dollar is a special character, you need to banalize them with a \

void main(){
    String s = "! @ # \$ & * ~";
    print('$s');
}

Upvotes: 24

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126654

The reason this is not working for you is because the dollar sign $ is used for template literals, which can be used to "interpolate the value of Dart expressions within strings".
When only evaluating identifiers, just a dollar sign followed by the variable name is enough: 'foo: $foo'
However, curly braces can be added to evaluate whole expressions: 'foo * bar: ${foo * bar}'

Having said that, you will need to escape the dollar sign using a backslash: '50\$'

For your example: Text('! @ # \$ & * ~', style: TextStyle(color: Colors.grey))

Upvotes: 4

Related Questions