Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

string - Difference between single and double quotes in Flutter/Dart

I know that single and double quotes have at least some level of equivelence in Dart. For example,

var myString = "Hello world";      // double quotes

and

var myString = 'Hello world';      // single quotes

have no programmatic difference to my knowledge.

I keep seeing them used seemingly interchangeably in various examples and in some documentation. I'm wondering if there is a subtle difference that I am missing or if there is a recommended style to follow, especially in Flutter.

This is a Q&A self answer after reading the Flutter and Dart style guides.

question from:https://stackoverflow.com/questions/54014913/difference-between-single-and-double-quotes-in-flutter-dart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Single and double quotes both work in Dart

final myString = 'hello';

is the same as

final myString = "hello";

Delimiters need to be escaped

Use a backslash to escape single quotes in a single quote string.

final myString = 'Bob's dog';            // Bob's dog

Same thing to escape double quotes in a double quote string.

final myString = "a "quoted" word";     // a "quoted" word

But no need to escape anything if the delimiter is different.

final myString = "Bob's dog";             // Bob's dog
final myString = 'a "quoted" word';       // a "quoted" word

Also no need to worry about the value passed into an interpolated string.

final value = '"quoted"';                 // "quoted"
final myString = "a $value word";         // a "quoted" word

Prefer single quotes in Flutter

The Flutter style guide recommends using single quotes for everything

final myString = 'hello';

except for nested strings

print('Hello ${name.split(" ")[0]}');

or strings containing single quotes (optional)

final myString = "Bob's dog";
final myString = 'Bob's dog';  // ok

The Dart style guide appears to be silent on the issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...