单引号和双引号在 Dart 中都适用
final myString = 'hello';
是相同的
final myString = "hello";
分隔符需要转义
Use a \
反斜杠用于转义单引号字符串中的单引号。
final myString = 'Bob\'s dog'; // Bob's dog
在双引号字符串中转义双引号也是如此。
final myString = "a \"quoted\" word"; // a "quoted" word
但如果分隔符不同,则无需转义任何内容。
final myString = "Bob's dog"; // Bob's dog
final myString = 'a "quoted" word'; // a "quoted" word
也无需担心传递到内插字符串中的值。
final value = '"quoted"'; // "quoted"
final myString = "a $value word"; // a "quoted" word
在 Flutter 中更喜欢单引号
The 颤振风格指南 https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#prefer-single-quotes-for-strings建议所有内容都使用单引号
final myString = 'hello';
除了嵌套字符串
print('Hello ${name.split(" ")[0]}');
或包含单引号的字符串(可选)
final myString = "Bob's dog";
final myString = 'Bob\'s dog'; // ok
The Dart 风格指南 https://www.dartlang.org/guides/language/effective-dart/usage#strings似乎对这个问题保持沉默。