跳到主要內容

Linter 規則

使用 Dart linter 來識別 Dart 程式碼中可能的問題。您可以使用 IDE 或 dart analyze 命令透過 linter 執行檢查。如需瞭解如何啟用和停用個別 linter 規則的資訊,請參閱分析器文件個別規則章節

本頁列出所有 linter 規則,以及詳細資訊,例如您可能想要使用每個規則的時機、觸發規則的程式碼模式,以及如何修正程式碼。

集合

#

為了避免需要個別選取相容的 linter 規則,請考慮從 linter 規則集開始,下列套件提供這些規則集:

lints
包含 Dart 團隊策劃的兩個規則集。我們建議至少使用 core 規則集,此規則集用於評分上傳至 pub.dev 的套件。或者,更好的做法是使用 recommended 規則集,它是 core 的超集,可識別其他問題並強制執行樣式和格式。如果您要撰寫 Flutter 程式碼,請使用 flutter_lints 套件中的規則集,此套件是以 lints 為基礎。

flutter_lints
包含 flutter 規則集,Flutter 團隊建議您在 Flutter 應用程式、套件和外掛程式中使用此規則集。此規則集是 recommended 集的超集,而 recommended 集本身又是 core 集的超集,core 集會部分決定上傳至 pub.dev 的套件的分數

若要瞭解如何使用特定規則集,請造訪啟用和停用 linter 規則的文件。

若要尋找更多預先定義的規則集,請查看 pub.dev 上的 #lints 主題

狀態

#

每個規則都有狀態或成熟度層級

穩定
這些規則可安全使用,並已驗證可在最新版本的 Dart 語言中正常運作。除非標示為實驗性、已淘汰或已移除,否則所有規則都視為穩定。
實驗性
這些規則仍在評估中,可能永遠不會穩定。請謹慎使用這些規則,並回報您遇到的任何問題。
已淘汰
不再建議使用這些規則,且可能會在未來的 Dart 版本中移除。
已移除
這些規則已在最新的穩定 Dart 版本中移除。

快速修正

#

有些規則可以使用快速修正自動修正。快速修正是針對修正 linter 規則回報的問題所做的自動化編輯。

如果規則有快速修正,則可以使用 dart fix 或使用您的具備 Dart 支援的編輯器來套用。若要瞭解詳情,請參閱分析問題的快速修正

規則

#

以下是所有 linter 規則的索引及其功能的簡短說明。若要瞭解特定規則的詳情,請按一下其卡片上的瞭解詳情按鈕。

如需包含 Dart 3.7.1 中所有 linter 規則的自動產生清單,請查看所有 linter 規則


always_declare_return_types

宣告方法傳回類型。

build
always_put_control_body_on_new_line

將控制結構運算式與其陳述式分開。

build
always_put_required_named_parameters_first

將必要的具名參數放在最前面。

build
always_require_non_null_named_parameters

在沒有預設值的具名參數上指定 @required

error
always_specify_types

指定類型註解。

build
always_use_package_imports

避免對 lib/ 中的檔案使用相對匯入。

build
annotate_overrides

註解覆寫的成員。

build thumb_up flutter
annotate_redeclares

註解重新宣告的成員。

science build
avoid_annotating_with_dynamic

在不需要時避免使用 dynamic 進行註解。

build
avoid_as

避免使用 as

error
avoid_bool_literals_in_conditional_expressions

避免在條件運算式中使用 bool 字面值。

avoid_catches_without_on_clauses

避免沒有 on 子句的 catch。

avoid_catching_errors

請勿明確捕捉 Error 或實作它的類型。

avoid_classes_with_only_static_members

避免定義僅包含靜態成員的類別。

avoid_double_and_int_checks

避免 doubleint 檢查。

avoid_dynamic_calls

避免在 dynamic 目標上進行方法呼叫或屬性存取。

avoid_empty_else

避免在 else 子句中使用空白陳述式。

build circles thumb_up flutter
avoid_equals_and_hash_code_on_mutable_classes

避免在未標示為 @immutable 的類別上多載運算子 == 和 hashCode。

avoid_escaping_inner_quotes

透過轉換周圍的引號來避免跳脫內部引號。

build
avoid_field_initializers_in_const_classes

避免在 const 類別中使用欄位初始設定式。

avoid_final_parameters

避免對參數宣告使用 final

avoid_function_literals_in_foreach_calls

避免在 forEach 中使用函式字面值。

build thumb_up flutter
avoid_futureor_void

避免使用 'FutureOr' 作為結果的類型。

science
avoid_implementing_value_types

請勿實作覆寫 == 的類別。

avoid_init_to_null

請勿將變數明確初始化為 null

build thumb_up flutter
avoid_js_rounded_ints

避免 JavaScript 四捨五入的整數。

avoid_multiple_declarations_per_line

請勿在單行上宣告多個變數。

build
avoid_null_checks_in_equality_operators

請勿在自訂 == 運算子中檢查 null

build
avoid_positional_boolean_parameters

避免位置布林參數。

avoid_print

避免在生產程式碼中呼叫 print

build flutter
avoid_private_typedef_functions

避免私有類型定義函式。

build
avoid_redundant_argument_values

避免多餘的引數值。

build
avoid_relative_lib_imports

避免對 lib/ 中的檔案使用相對匯入。

build circles thumb_up flutter
avoid_renaming_method_parameters

請勿重新命名覆寫方法的參數。

build thumb_up flutter
avoid_return_types_on_setters

避免 setter 的傳回類型。

build thumb_up flutter
avoid_returning_null

避免從傳回類型為 bool、double、int 或 num 的成員傳回 null。

error
avoid_returning_null_for_future

避免為 Future 傳回 null。

error
avoid_returning_null_for_void

避免為 void 傳回 null

build thumb_up flutter
avoid_returning_this

避免僅為了啟用流暢介面而從方法傳回 this。

avoid_setters_without_getters

避免沒有 getter 的 setter。

avoid_shadowing_type_parameters

避免遮蔽類型參數。

circles thumb_up flutter
avoid_single_cascade_in_expression_statements

避免在運算式陳述式中使用單一串聯。

build thumb_up flutter
avoid_slow_async_io

避免速度緩慢的非同步 dart:io 方法。

avoid_type_to_string

避免在生產程式碼中使用.toString(),因為結果可能會被最小化。

avoid_types_as_parameter_names

避免使用類型作為參數名稱。

build circles thumb_up flutter
avoid_types_on_closure_parameters

避免為函式運算式參數註解類型。

build
avoid_unnecessary_containers

避免不必要的容器。

build flutter
avoid_unstable_final_fields

避免覆寫 final 欄位以在多次呼叫時傳回不同的值。

error
avoid_unused_constructor_parameters

避免在建構子中定義未使用的參數。

build
avoid_void_async

避免傳回 voidasync 函式。

build
avoid_web_libraries_in_flutter

避免在 Flutter Web 外掛程式套件外部使用僅限 Web 的函式庫。

flutter
await_only_futures

僅等候 futures。

build circles thumb_up flutter
camel_case_extensions

使用 UpperCamelCase 命名擴充功能。

circles thumb_up flutter
camel_case_types

使用 UpperCamelCase 命名類型。

circles thumb_up flutter
cancel_subscriptions

取消 dart:async StreamSubscription 的執行個體。

cascade_invocations

在相同的參考上串聯連續的方法調用。

build
cast_nullable_to_non_nullable

請勿將可為 null 的值轉換為不可為 null 的類型。

build
close_sinks

關閉 dart:core Sink 的執行個體。

collection_methods_unrelated_type

使用不相關類型的引數調用各種集合方法。

circles thumb_up flutter
combinators_ordering

依字母順序排序組合器名稱。

build
comment_references

僅在文件註解中參考範圍內的識別項。

build
conditional_uri_does_not_exist

遺失條件式匯入。

constant_identifier_names

偏好對常數名稱使用 lowerCamelCase。

build thumb_up flutter
control_flow_in_finally

避免在 finally 區塊中使用控制流程。

thumb_up flutter
curly_braces_in_flow_control_structures

針對所有流程控制結構「務必」使用大括號。

build circles thumb_up flutter
dangling_library_doc_comments

將函式庫文件註解附加至函式庫指示詞。

build circles thumb_up flutter
depend_on_referenced_packages

相依於參考的套件。

circles thumb_up flutter
deprecated_consistency

遺失 deprecated 註解。

deprecated_member_use_from_same_package

避免從宣告已淘汰元素的套件內使用這些元素。

build
diagnostic_describe_all_properties

「務必」在偵錯方法中參考所有公用屬性。

build
directives_ordering

遵守 Effective Dart Guide 指示詞排序慣例。

build
discarded_futures

請勿在非 async 區塊中調用非同步函式。

build
do_not_use_environment

請勿使用環境宣告的變數。

document_ignores

為忽略註解加上文件。

empty_catches

避免空白 catch 區塊。

build circles thumb_up flutter
empty_constructor_bodies

針對空白建構子主體使用 ; 而非 {}

build thumb_up flutter
empty_statements

避免空白陳述式。

build thumb_up flutter
enable_null_safety

「務必」使用健全的 Null 安全性。

error
eol_at_end_of_file

在檔案結尾放置單一換行符號。

build
exhaustive_cases

為類別 enum-like 中的所有常數定義 case 子句。

build thumb_up flutter
file_names

使用 lowercase_with_underscores 命名原始碼檔案。

circles thumb_up flutter
flutter_style_todos

使用 Flutter TODO 格式:// TODO(使用者名稱):訊息,https://URL-to-issue。

build
hash_and_equals

如果覆寫 ==,則一律覆寫 hashCode

build circles thumb_up flutter
implementation_imports

請勿從另一個套件匯入實作檔案。

thumb_up flutter
implicit_call_tearoffs

在將物件當作函式使用時,明確提取 call 方法。

build circles thumb_up flutter
implicit_reopen

請勿隱含地重新開啟類別。

science build
invalid_case_patterns

使用在 Dart 3.0 中有效的 case 運算式。

science build
invalid_runtime_check_with_js_interop_types

避免對 JS 互通性類型進行執行階段類型測試,因為結果可能與平台不一致。

thumb_up flutter
invariant_booleans

條件不應無條件評估為 truefalse

error
iterable_contains_unrelated_type

使用不相關類型的參考調用 Iterable<E>.contains

error
join_return_with_assignment

盡可能將 return 陳述式與指派結合。

leading_newlines_in_multiline_strings

以換行符號開始多行字串。

build
library_annotations

將函式庫註解附加至函式庫指示詞。

build circles thumb_up flutter
library_names

使用 lowercase_with_underscores 命名函式庫。

library_prefixes

在指定函式庫前置字元時使用 lowercase_with_underscores

thumb_up flutter
library_private_types_in_public_api

避免在公用 API 中使用私有類型。

thumb_up flutter
lines_longer_than_80_chars

避免行長度超過 80 個字元。

list_remove_unrelated_type

使用不相關類型的參考調用 remove

error
literal_only_boolean_expressions

僅由字面值組成的布林運算式。

matching_super_parameters

使用相符的 super 參數名稱。

missing_code_block_language_in_doc_comment

程式碼區塊遺失指定的語言。

missing_whitespace_between_adjacent_strings

相鄰字串之間遺失空格。

no_adjacent_strings_in_list

請勿在清單中使用相鄰字串。

no_default_cases

沒有預設 case。

science
no_duplicate_case_values

請勿使用多個具有相同值的 case。

build circles thumb_up flutter
no_leading_underscores_for_library_prefixes

避免函式庫前置字元使用開頭底線。

build thumb_up flutter
no_leading_underscores_for_local_identifiers

避免本機識別項使用開頭底線。

build thumb_up flutter
no_literal_bool_comparisons

請勿將布林運算式與布林字面值進行比較。

build
no_logic_in_create_state

請勿在 createState 中放置任何邏輯。

flutter
no_runtimetype_tostring

避免在 runtimeType 上呼叫 toString()

no_self_assignments

請勿將變數指派給自己。

no_wildcard_variable_uses

請勿使用萬用字元參數或變數。

circles thumb_up flutter
non_constant_identifier_names

使用 lowerCamelCase 命名非常數識別項。

build circles thumb_up flutter
noop_primitive_operations

Noop 原始作業。

build
null_check_on_nullable_type_parameter

請勿在可能為 null 的類型參數上使用 null 檢查。

build circles thumb_up flutter
null_closures

在預期使用閉包的位置,請勿傳遞 null 作為引數。

build thumb_up flutter
omit_local_variable_types

省略本機變數的類型註解。

build
omit_obvious_local_variable_types

省略本機變數的明顯類型註解。

science build
omit_obvious_property_types

省略最上層和靜態變數的明顯類型註解。

science build
one_member_abstracts

當簡單函式即可完成時,避免定義單一成員抽象類別。

only_throw_errors

僅擲回擴充 Exception 或 Error 的類別執行個體。

overridden_fields

請勿覆寫欄位。

thumb_up flutter
package_api_docs

為所有公用 API 提供文件註解。

error
package_names

對套件名稱使用 lowercase_with_underscores

thumb_up flutter
package_prefixed_library_names

使用套件名稱和點分隔路徑作為函式庫名稱的前置字元。

parameter_assignments

請勿重新指派函式或方法的參數參考。

prefer_adjacent_string_concatenation

使用相鄰字串來串連字串字面值。

build thumb_up flutter
prefer_asserts_in_initializer_lists

偏好將 asserts 放在初始設定式清單中。

prefer_asserts_with_message

偏好使用含訊息的 asserts。

prefer_bool_in_asserts

偏好使用布林值作為 assert 條件。

error
prefer_collection_literals

盡可能使用集合字面值。

build thumb_up flutter
prefer_conditional_assignment

偏好使用 ??= 而非測試 null

build thumb_up flutter
prefer_const_constructors

偏好對常數建構子使用 const

build
prefer_const_constructors_in_immutables

偏好在 @immutable 類別上宣告 const 建構子。

build flutter
prefer_const_declarations

針對宣告,偏好使用 const 而非 final

build
prefer_const_literals_to_create_immutables

偏好使用 const 字面值作為 @immutable 類別上建構子的參數。

build
prefer_constructors_over_static_methods

偏好定義建構子而非靜態方法來建立執行個體。

prefer_contains

針對 ListString 執行個體使用 contains。

build thumb_up flutter
prefer_double_quotes

在不需要跳脫序列的情況下,偏好使用雙引號。

build
prefer_equal_for_default_values

使用 = 將具名參數與其預設值分開。

error
prefer_expression_function_bodies

針對主體為單一 return 陳述式的簡短成員,使用 =>。

build
prefer_final_fields

私有欄位可以是 final

build thumb_up flutter
prefer_final_in_for_each

如果未重新指派參考,則偏好在 for-each 迴圈變數中使用 final。

build
prefer_final_locals

如果未重新指派變數宣告,則偏好使用 final。

build
prefer_final_parameters

如果未重新指派參數宣告,則偏好使用 final。

build
prefer_for_elements_to_map_fromiterable

從可迭代物件建置 map 時,偏好使用 for 元素。

build thumb_up flutter
prefer_foreach

僅將函式套用至所有元素時,使用 forEach

prefer_function_declarations_over_variables

使用函式宣告將函式繫結至名稱。

build thumb_up flutter
prefer_generic_function_type_aliases

偏好泛型函式類型別名。

build circles thumb_up flutter
prefer_if_elements_to_conditional_expressions

盡可能偏好使用 if 元素而非條件運算式。

build
prefer_if_null_operators

偏好使用 ?? 運算子。

build thumb_up flutter
prefer_initializing_formals

盡可能使用初始設定形式參數。

build thumb_up flutter
prefer_inlined_adds

盡可能內嵌清單項目宣告。

build thumb_up flutter
prefer_int_literals

偏好使用整數字面值而非雙精準度字面值。

build
prefer_interpolation_to_compose_strings

使用插值來組合字串和值。

build thumb_up flutter
prefer_is_empty

針對 IterableMap 使用 isEmpty

build circles thumb_up flutter
prefer_is_not_empty

針對 IterableMap 使用 isNotEmpty

build circles thumb_up flutter
prefer_is_not_operator

偏好使用 is! 運算子。

build thumb_up flutter
prefer_iterable_wheretype

偏好在可迭代物件上使用 whereType

build circles thumb_up flutter
prefer_mixin

偏好使用 mixin。

prefer_null_aware_method_calls

偏好使用 Null 感知方法呼叫。

prefer_null_aware_operators

偏好使用 Null 感知運算子。

build thumb_up flutter
prefer_relative_imports

針對 lib/ 中的檔案,偏好使用相對匯入。

build
prefer_single_quotes

僅針對包含單引號的字串使用雙引號。

build
prefer_spread_collections

盡可能使用擴展集合。

build thumb_up flutter
prefer_typing_uninitialized_variables

偏好為未初始化的變數和欄位輸入類型。

build circles thumb_up flutter
prefer_void_to_null

除非您確定不想要 void,否則請勿使用 Null 類型。

build
provide_deprecation_message

透過 @Deprecated("message") 提供淘汰訊息。

circles thumb_up flutter
public_member_api_docs

為所有公用成員加上文件。

recursive_getters

屬性 getter 遞迴傳回自身。

thumb_up flutter
require_trailing_commas

針對所有參數清單和引數清單使用結尾逗號。

build
secure_pubspec_urls

pubspec.yaml 中使用安全網址。

circles thumb_up flutter
sized_box_for_whitespace

用於空格的 SizedBox

build flutter
sized_box_shrink_expand

使用 SizedBox shrink 和 expand 具名建構子。

slash_for_doc_comments

偏好使用 /// 作為文件註解。

build thumb_up flutter
sort_child_properties_last

在小工具執行個體建立中,將子屬性排序在最後。

build flutter
sort_constructors_first

將建構子宣告排序在其他成員之前。

build
sort_pub_dependencies

依字母順序排序 pub 相依性。

sort_unnamed_constructors_first

將未命名的建構子宣告排序在最前面。

build
specify_nonobvious_local_variable_types

為不明顯的本機變數類型指定類型註解。

science build
specify_nonobvious_property_types

為不明顯的最上層和靜態變數類型指定類型註解。

science build
strict_top_level_inference

指定類型註解。

build
super_goes_last

super 呼叫放在建構子初始設定清單的最後面。

error
test_types_in_equals

測試 operator ==(Object other) 中引數的類型。

throw_in_finally

避免在 finally 區塊中使用 throw

tighten_type_of_initializing_formals

強化初始化形式參數的型別。

type_annotate_public_apis

為公開 API 加上型別註解。

build
type_init_formals

不要為初始化形式參數加上型別註解。

build thumb_up flutter
type_literal_in_constant_pattern

請勿將常數模式與型別字面值一起使用。

build circles thumb_up flutter
unawaited_futures

async 函式主體中的 Future 結果必須使用 await 等待,或使用 dart:async 標記為 unawaited

build
unintended_html_in_doc_comment

在文件註解中使用角括號會被 Markdown 當作 HTML 處理。

circles thumb_up flutter
unnecessary_async

沒有 await 就沒有 async。

science
unnecessary_await_in_return

在 return 中不必要的 await 關鍵字。

build
unnecessary_brace_in_string_interps

避免在不需要時於插值中使用大括號。

build thumb_up flutter
unnecessary_breaks

當 break 是隱含的時候,請勿使用明確的 break

build
unnecessary_const

避免使用 const 關鍵字。

build thumb_up flutter
unnecessary_constructor_name

不必要的 .new 建構子名稱。

build thumb_up flutter
unnecessary_final

請勿對區域變數使用 final

build
unnecessary_getters_setters

避免為了「安全」而將欄位包裝在 getter 和 setter 中。

build thumb_up flutter
unnecessary_ignore

請勿忽略未產生的診斷碼。

science build
unnecessary_lambdas

當 tear-off 可以做到時,請勿建立 lambda。

build
unnecessary_late

當不需要時,請勿指定 late 修飾詞。

build thumb_up flutter
unnecessary_library_directive

除非程式庫指令具有文件註解或註解,否則請避免使用。

build
unnecessary_library_name

請勿在 library 宣告中包含程式庫名稱。

build thumb_up flutter
unnecessary_new

不必要的 new 關鍵字。

build thumb_up flutter
unnecessary_null_aware_assignments

在可空感知賦值中避免使用 null

build thumb_up flutter
unnecessary_null_aware_operator_on_extension_on_nullable

在可空型別擴充功能上不必要的可空感知運算子。

unnecessary_null_checks

不必要的 null 檢查。

science build
unnecessary_null_in_if_null_operators

避免在 ?? 運算子中使用 null

build thumb_up flutter
unnecessary_nullable_for_final_variable_declarations

為以非可為 null 值初始化的 final 變數使用非可為 null 型別。

build thumb_up flutter
unnecessary_overrides

請勿覆寫方法以使用相同的參數執行 super 方法調用。

build circles thumb_up flutter
unnecessary_parenthesis

可以移除不必要的括號。

build
unnecessary_raw_strings

不必要的原始字串。

build
unnecessary_statements

避免使用不必要的陳述式。

unnecessary_string_escapes

移除字串中不必要的反斜線。

build thumb_up flutter
unnecessary_string_interpolations

不必要的字串插值。

build thumb_up flutter
unnecessary_this

除非為了避免遮蔽,否則請勿使用 this 存取成員。

build thumb_up flutter
unnecessary_to_list_in_spreads

展開運算子中不必要的 toList()

build thumb_up flutter
unnecessary_underscores

可以移除不必要的底線。

build thumb_up flutter
unreachable_from_main

可執行程式庫中無法存取的最上層成員。

build
unrelated_type_equality_checks

使用不相關型別的參考調用相等運算子 ==

circles thumb_up flutter
unsafe_html

避免使用不安全的 HTML API。

error
unsafe_variance

不安全的型別:在非協變位置中有型別變數。

science
use_build_context_synchronously

請勿跨非同步間隙使用 BuildContext

flutter
use_colored_box

使用 ColoredBox

build
use_decorated_box

使用 DecoratedBox

build
use_enums

使用列舉,而不是行為類似列舉的類別。

build
use_full_hex_values_for_flutter_colors

偏好使用 8 位數十六進位整數 (例如,0xFFFFFFFF) 來實例化 Color。

build flutter
use_function_type_syntax_for_parameters

為參數使用泛型函數型別語法。

build thumb_up flutter
use_if_null_to_convert_nulls_to_bools

使用 ?? 運算子將 null 轉換為 bool

use_is_even_rather_than_modulo

偏好使用 intValue.isOdd/isEven 而不是檢查 % 2 的結果。

use_key_in_widget_constructors

在 widget 建構子中使用 key。

build flutter
use_late_for_private_fields_and_variables

對於具有非可空型別的私有成員,請使用 late。

science
use_named_constants

使用預定義的具名常數。

build
use_raw_strings

使用原始字串以避免跳脫字元。

build
use_rethrow_when_possible

使用 rethrow 重新拋出捕獲的例外。

build thumb_up flutter
use_setters_to_change_properties

對於概念上會變更屬性的操作,請使用 setter。

use_string_buffers

使用字串緩衝區來組合字串。

use_string_in_part_of_directives

在 part of 指令中使用字串。

build circles thumb_up flutter
use_super_parameters

盡可能使用 super 初始化子參數。

science build thumb_up flutter
use_test_throws_matchers

使用 throwsA matcher 而不是 fail()。

use_to_and_as_if_applicable

如果適用,方法名稱請以 to/_to 或 as/_as 開頭。

use_truncating_division

使用截斷除法。

build
valid_regexps

使用有效的正規表示式語法。

circles thumb_up flutter
void_checks

請勿賦值給 void

circles thumb_up flutter