prefer_double_quotes
在不需要转义序列的地方,优先使用双引号。
详情
#应该在不需要额外转义的地方使用双引号。
这意味着包含双引号的字符串可以使用单引号,这样双引号就不用转义了(注意:我们不会反过来检查,即,包含转义双引号的双引号字符串不会被标记)。
字符串插值中也可能包含字符串,虽然这种情况比较少见。在这种情况下,在某处使用单引号会更易读。因此,在插值字符串字面量内部或外部都允许使用单引号。可以说,字符串插值内部的字符串应该有其自身的 lint 类型。
错误示例
dart
useStrings(
'should be double quote',
r'should be double quote',
r\'''should be double quotes\''')
正确示例
dart
useStrings(
"should be double quote",
r"should be double quote",
r"""should be double quotes""",
'ok with " inside',
'nested \${a ? "strings" : "can"} be wrapped by a double quote',
"and nested \${a ? 'strings' : 'can be double quoted themselves'}");
不兼容的规则
#prefer_double_quotes
规则与以下规则不兼容
启用
#要启用 prefer_double_quotes
规则,请在你的 analysis_options.yaml
文件中的 linter > rules 下添加 prefer_double_quotes
。
analysis_options.yaml
yaml
linter:
rules:
- prefer_double_quotes
如果你使用的是 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 prefer_double_quotes: true
。
analysis_options.yaml
yaml
linter:
rules:
prefer_double_quotes: true
除非另有说明,否则本站点上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。