目录

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:

dart
useStrings(
    "should be single quote",
    r"should be single quote",
    r"""should be single quotes""")

GOOD:

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 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 prefer_single_quotes

analysis_options.yaml
yaml
linter:
  rules:
    - prefer_single_quotes