prefer_single_quotes
仅对包含单引号的字符串使用双引号。
详情
#应该 在不需要额外转义的情况下使用单引号。
这意味着带有撇号的字符串可以使用双引号,这样撇号就不需要转义(注意:我们不进行反向 lint,即带有转义撇号的单引号字符串不会被标记)。
在字符串插值中包含字符串的情况也很少见,但有可能发生。在这种情况下,在某处使用双引号会更具可读性。因此,允许在插值字符串字面量内部或包含它的地方使用双引号。可以说,字符串插值中的字符串应该有自己的 lint 类型。
错误
dart
useStrings(
"should be single quote",
r"should be single quote",
r"""should be single quotes""")
正确
dart
useStrings(
'should be single quote',
r'should be single quote',
r\'''should be single quotes\''',
"here's ok",
"nested \${a ? 'strings' : 'can'} be wrapped by a double quote",
'and nested \${a ? "strings" : "can be double quoted themselves"}');
不兼容的规则
#prefer_single_quotes
规则与以下规则不兼容
启用
#要启用 prefer_single_quotes
规则,请在您的 analysis_options.yaml
文件中的 linter > rules 下添加 prefer_single_quotes
analysis_options.yaml
yaml
linter:
rules:
- prefer_single_quotes
如果您改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 prefer_single_quotes: true
analysis_options.yaml
yaml
linter:
rules:
prefer_single_quotes: true
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。