跳到主要內容

prefer_single_quotes

穩定
提供修正

字串中僅在包含單引號時使用雙引號。

詳細資訊

#

應該 在不需要額外跳脫字元的情況下使用單引號。

這表示包含撇號的字串可以使用雙引號,這樣撇號就不需要跳脫字元 (注意:我們不會反向檢查,也就是說,使用跳脫字元的單引號字串不會被標記)。

在字串插值中包含字串的情況也很少見,但有可能發生。在這種情況下,在某處使用雙引號會更易於閱讀。因此,允許在插值字串字面值內部或包含它的情況下使用雙引號。可以說,字串插值中的字串應該有自己的 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 map 語法來設定 linter 規則,請在 linter > rules 下新增 prefer_single_quotes: true

analysis_options.yaml
yaml
linter:
  rules:
    prefer_single_quotes: true