跳至主內容

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