跳至主内容

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