prefer_single_quotes
Only use double quotes for strings containing single quotes.
此规则自 Dart 2.0 版本起可用。
此规则提供 快速修复 。
_不兼容规则:prefer_double_quotes _
详情
#DO use single quotes where they wouldn't require additional escapes.
That means strings with an apostrophe may use double quotes so that the apostrophe isn't escaped (note: we don't lint the other way around, ie, a single quoted string with an escaped apostrophe is not flagged).
It's also rare, but possible, to have strings within string interpolations. In this case, it's much more readable to use a double quote somewhere. So double quotes are allowed either within, or containing, an interpolated string literal. Arguably strings within string interpolations should be its own type of lint.
BAD:
useStrings(
"should be single quote",
r"should be single quote",
r"""should be single quotes""")
GOOD:
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
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 prefer_single_quotes
:
linter:
rules:
- prefer_single_quotes
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.