目錄

prefer_single_quotes

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

此規則自 Dart 2.0 起可用。

此規則有快速修復可用。

不相容的規則:prefer_double_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 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 prefer_single_quotes

analysis_options.yaml
yaml
linter:
  rules:
    - prefer_single_quotes