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
集合的超集,它本身是core
集合的超集,該集合部分決定了上傳到 pub.dev 的套件的分數。
若要了解如何使用特定規則集,請參閱啟用和停用 linter 規則的文件。
若要尋找更多預定義的規則集,請查看 pub.dev 上的 #lints
主題。
狀態
#每個規則都有一個狀態或成熟度級別
- 穩定
- 這些規則可以安全使用,並已驗證可與最新版本的 Dart 語言一起使用。除非標記為實驗性、已棄用或已移除,否則所有規則都視為穩定。
- 實驗性
- 這些規則仍在評估中,可能永遠不會穩定。請謹慎使用這些規則,並報告您遇到的任何問題。
- 已棄用
- 不再建議使用這些規則,並可能在未來的 Dart 版本中移除。
- 已移除
- 這些規則已在最新的穩定 Dart 版本中移除。
快速修正
#有些規則可以使用快速修正自動修正。快速修正是針對修正 linter 規則報告的問題的自動化編輯。
如果規則有快速修正,可以使用 dart fix
或使用您的支援 Dart 的編輯器來應用它。若要深入了解,請參閱分析問題的快速修正。
規則
#以下是所有 linter 規則的索引以及其功能的簡短說明。若要深入了解每個規則,請按一下其名稱。
如需包含 Dart 3.6.0
中所有 linter 規則的自動產生清單,請查看所有 linter 規則。
always_declare_return_types
宣告方法回傳類型。
always_put_control_body_on_new_line
將控制結構運算式與其陳述式分開。
always_put_required_named_parameters_first
將必要具名參數放在最前面。
always_require_non_null_named_parameters
(已移除)
在沒有預設值的具名參數上指定 @required
。
always_specify_types
指定類型註釋。
always_use_package_imports
避免 lib/
中檔案的相對匯入。
annotate_overrides
註釋覆寫的成員。
annotate_redeclares
(實驗性)
註釋重新宣告的成員。
avoid_annotating_with_dynamic
在不需要時避免使用 dynamic
進行註釋。
avoid_as
(已移除)
避免使用 as
。
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
避免 double
和 int
檢查。
avoid_dynamic_calls
避免對 dynamic
目標進行方法呼叫或屬性存取。
avoid_empty_else
避免在 else 子句中使用空陳述式。
avoid_equals_and_hash_code_on_mutable_classes
避免在未標記為 @immutable
的類別上多載運算子 == 和 hashCode。
avoid_escaping_inner_quotes
透過轉換周圍的引號來避免逸出內部的引號。
avoid_field_initializers_in_const_classes
避免在 const 類別中使用欄位初始化器。
avoid_final_parameters
避免參數宣告使用 final
。
avoid_function_literals_in_foreach_calls
避免在 forEach
中使用函式常值。
avoid_futureor_void
(實驗性)
避免使用 'FutureOr
avoid_implementing_value_types
不要實作覆寫 ==
的類別。
avoid_init_to_null
不要明確將變數初始化為 null
。
avoid_js_rounded_ints
避免 JavaScript 四捨五入的整數。
avoid_multiple_declarations_per_line
不要在單行中宣告多個變數。
avoid_null_checks_in_equality_operators
不要在自訂的 ==
運算子中檢查 null
。
avoid_positional_boolean_parameters
避免使用位置布林參數。
avoid_print
避免在生產環境程式碼中使用 print
呼叫。
avoid_private_typedef_functions
避免使用私有的 typedef 函式。
avoid_redundant_argument_values
避免多餘的參數值。
avoid_relative_lib_imports
避免 lib/
中檔案的相對匯入。
avoid_renaming_method_parameters
不要重新命名覆寫方法中的參數。
avoid_return_types_on_setters
避免在 setter 上使用回傳型別。
avoid_returning_null
(已移除)
避免從回傳型別為 bool、double、int 或 num 的成員回傳 null。
avoid_returning_null_for_future
(已移除)
避免為 Future 回傳 null。
avoid_returning_null_for_void
避免為 void
回傳 null
。
avoid_returning_this
避免為了啟用流暢介面而從方法中回傳 this。
avoid_setters_without_getters
避免使用沒有 getter 的 setter。
avoid_shadowing_type_parameters
避免遮蔽型別參數。
avoid_single_cascade_in_expression_statements
避免在表達式語句中使用單一級聯。
avoid_slow_async_io
避免使用緩慢的非同步 dart:io
方法。
avoid_type_to_string
避免
avoid_types_as_parameter_names
避免使用型別作為參數名稱。
avoid_types_on_closure_parameters
避免為函式表達式參數註解型別。
avoid_unnecessary_containers
避免不必要的容器。
avoid_unstable_final_fields
(已移除)
避免覆寫 final 欄位以在多次呼叫時回傳不同的值。
avoid_unused_constructor_parameters
避免在建構子中定義未使用的參數。
avoid_void_async
避免回傳 void
的 async
函式。
avoid_web_libraries_in_flutter
避免在 Flutter web 外掛套件之外使用僅限網頁的程式庫。
await_only_futures
只等待 future。
camel_case_extensions
使用 UpperCamelCase 為擴展命名。
camel_case_types
使用 UpperCamelCase 為型別命名。
cancel_subscriptions
取消 dart:async
的 StreamSubscription
執行個體。
cascade_invocations
在同一個參考上級聯連續的方法呼叫。
cast_nullable_to_non_nullable
不要將可為 null 的值轉換為不可為 null 的型別。
close_sinks
關閉 dart:core
的 Sink
執行個體。
collection_methods_unrelated_type
使用不相關型別的引數呼叫各種集合方法。
combinators_ordering
依字母順序排序組合器名稱。
comment_references
只在文件註解中參考範圍內的識別項。
conditional_uri_does_not_exist
缺少條件匯入。
constant_identifier_names
常數名稱偏好使用 lowerCamelCase。
control_flow_in_finally
避免在 finally
區塊中使用控制流程。
curly_braces_in_flow_control_structures
所有流程控制結構都「應該」使用大括號。
dangling_library_doc_comments
將程式庫文件註解附加到程式庫指令。
depend_on_referenced_packages
依賴於參考的套件。
deprecated_consistency
缺少已棄用註解。
deprecated_member_use_from_same_package
避免在宣告已棄用元素的套件內使用它們。
diagnostic_describe_all_properties
「應該」在除錯方法中參考所有公開屬性。
directives_ordering
遵守 Effective Dart Guide 指令排序慣例。
discarded_futures
不要在非 async
區塊中呼叫非同步函式。
do_not_use_environment
不要使用環境宣告的變數。
document_ignores
文件忽略註解。
empty_catches
避免空的 catch 區塊。
empty_constructor_bodies
空的建構子主體請使用 ;
而不是 {}
。
empty_statements
避免使用空語句。
enable_null_safety
(已移除)
請使用健全的空值安全機制。
eol_at_end_of_file
在檔案結尾放置一個單一換行符號。
exhaustive_cases
為類似列舉類別中的所有常數定義 case 子句。
file_names
使用 lowercase_with_underscores
為原始碼檔案命名。
flutter_style_todos
使用 Flutter TODO 格式:// TODO(username): message, https://URL-to-issue。
hash_and_equals
如果覆寫 ==
,請務必覆寫 hashCode
。
implementation_imports
不要從另一個套件匯入實作檔案。
implicit_call_tearoffs
將物件當作函式使用時,明確地分離 call
方法。
implicit_reopen
(實驗性)
不要隱式重新開啟類別。
invalid_case_patterns
(實驗性)
使用在 Dart 3.0 中有效的 case 表達式。
invalid_runtime_check_with_js_interop_types
避免在結果可能與平台不一致的情況下,使用 JS 互通型別進行執行階段型別測試。
invariant_booleans
(已移除)
條件不應無條件評估為 true
或 false
。
iterable_contains_unrelated_type
(已移除)
使用不相關型別的參考呼叫 Iterable<E>.contains
。
join_return_with_assignment
盡可能將 return 語句與指派合併。
leading_newlines_in_multiline_strings
以換行符號開頭多行字串。
library_annotations
將程式庫註解附加到程式庫指令。
library_names
使用 lowercase_with_underscores
為程式庫命名。
library_prefixes
指定程式庫前置詞時請使用 lowercase_with_underscores
。
library_private_types_in_public_api
避免在公開 API 中使用私有型別。
lines_longer_than_80_chars
避免使用超過 80 個字元的行。
list_remove_unrelated_type
(已移除)
使用不相關型別的參考呼叫 remove
。
literal_only_boolean_expressions
只由常值組成的布林表達式。
matching_super_parameters
使用相符的超參數名稱。
missing_code_block_language_in_doc_comment
程式碼區塊缺少指定的語言。
missing_whitespace_between_adjacent_strings
相鄰字串之間缺少空格。
no_adjacent_strings_in_list
請勿在清單中使用相鄰的字串。
no_default_cases
(實驗性)
沒有預設 case。
no_duplicate_case_values
請勿使用多個具有相同值的 case。
no_leading_underscores_for_library_prefixes
避免在程式庫前置詞中使用前置底線。
no_leading_underscores_for_local_identifiers
避免在區域識別項中使用前置底線。
no_literal_bool_comparisons
請勿將布林表達式與布林常值進行比較。
no_logic_in_create_state
不要在 createState 中放置任何邏輯。
no_runtimeType_toString
避免在 runtimeType
上呼叫 toString()
。
no_self_assignments
請勿將變數指派給自己。
no_wildcard_variable_uses
不要使用萬用字元參數或變數。
non_constant_identifier_names
非常數識別項請使用 lowerCamelCase 命名。
noop_primitive_operations
無運算的原始運算。
null_check_on_nullable_type_parameter
不要在可能為 null 的型別參數上使用 null
檢查。
null_closures
請勿將 null
當做預期為閉包的引數傳遞。
omit_local_variable_types
省略區域變數的型別註解。
omit_obvious_local_variable_types
(實驗性)
省略區域變數的明顯型別註解。
omit_obvious_property_types
(未發佈)
省略頂層和靜態變數的明顯型別註解。
one_member_abstracts
當簡單的函式即可完成時,請避免定義單一成員抽象類別。
only_throw_errors
只擲回擴展 Exception 或 Error 的類別執行個體。
overridden_fields
不要覆寫欄位。
package_api_docs
(已棄用)
為所有公開 API 提供文件註解。
package_names
套件名稱請使用 lowercase_with_underscores
。
package_prefixed_library_names
使用套件名稱和以點號分隔的路徑為程式庫名稱加上前置詞。
parameter_assignments
不要重新指派函式或方法的參數參考。
prefer_adjacent_string_concatenation
使用相鄰的字串來串連字串常值。
prefer_asserts_in_initializer_lists
偏好將 assert 放在初始化列表。
prefer_asserts_with_message
偏好使用帶有訊息的 assert。
prefer_bool_in_asserts
(已移除)
偏好使用布林值作為 assert 條件。
prefer_collection_literals
盡可能使用集合常值。
prefer_conditional_assignment
偏好使用 ??=
來測試 null
。
prefer_const_constructors
在常數建構子中使用 const
。
prefer_const_constructors_in_immutables
偏好在 @immutable
類別上宣告 const
建構子。
prefer_const_declarations
宣告時偏好使用 const
而不是 final
。
prefer_const_literals_to_create_immutables
偏好使用常數常值作為 @immutable
類別上建構子的參數。
prefer_constructors_over_static_methods
偏好定義建構子而不是靜態方法來建立執行個體。
prefer_contains
對 List
和 String
執行個體使用 contains。
prefer_double_quotes
偏好使用不需要跳脫序列的雙引號。
prefer_equal_for_default_values
(已移除)
使用 =
將具名參數與其預設值分隔開。
prefer_expression_function_bodies
對於主體為單一 return 語句的短成員,請使用 =>。
prefer_final_fields
私有欄位可能是 final
。
prefer_final_in_for_each
如果沒有重新指派參考,則偏好在 for-each 迴圈變數中使用 final。
prefer_final_locals
如果沒有重新指派,則變數宣告偏好使用 final。
prefer_final_parameters
如果沒有重新指派,則參數宣告偏好使用 final。
prefer_for_elements_to_map_fromIterable
從可迭代物件建立對應時,偏好使用 for
元素。
prefer_foreach
只將函式套用至所有元素時,請使用 forEach
。
prefer_function_declarations_over_variables
使用函式宣告將函式繫結至名稱。
prefer_generic_function_type_aliases
偏好使用泛型函式型別別名。
prefer_if_elements_to_conditional_expressions
在可能的情況下,偏好使用 if 元素而不是條件表達式。
prefer_if_null_operators
偏好使用 ??
運算子。
prefer_initializing_formals
盡可能使用初始化形式。
prefer_inlined_adds
盡可能內嵌清單項目宣告。
prefer_int_literals
偏好使用 int 常值而不是 double 常值。
prefer_interpolation_to_compose_strings
使用內插來組合字串和值。
prefer_is_empty
對 Iterable
和 Map
使用 isEmpty
。
prefer_is_not_empty
對 Iterable
和 Map
使用 isNotEmpty
。
prefer_is_not_operator
偏好使用 is! 運算子。
prefer_iterable_whereType
偏好在可迭代物件上使用 whereType
。
prefer_mixin
偏好使用混入。
prefer_null_aware_method_calls
偏好使用可為 null
的感知方法呼叫。
prefer_null_aware_operators
偏好使用可為 null
的感知運算子。
prefer_relative_imports
lib/
中檔案偏好使用相對匯入。
prefer_single_quotes
只在包含單引號的字串中使用雙引號。
prefer_spread_collections
盡可能使用展開集合。
prefer_typing_uninitialized_variables
偏好為未初始化的變數和欄位輸入型別。
prefer_void_to_null
除非您確定不需要 void,否則請勿使用 Null 型別。
provide_deprecation_message
透過 @Deprecated("message")
提供棄用訊息。
public_member_api_docs
為所有公開成員加上文件。
recursive_getters
屬性 getter 遞迴地回傳自身。
require_trailing_commas
所有參數列表和引數列表皆使用尾隨逗號。
secure_pubspec_urls
在 pubspec.yaml
中使用安全網址。
sized_box_for_whitespace
使用 SizedBox
作為空白間隔。
sized_box_shrink_expand
使用 SizedBox 的 shrink 和 expand 具名建構子。
slash_for_doc_comments
偏好使用 ///
作為文件註解。
sort_child_properties_last
在 widget 實例創建中,將子屬性排序至最後。
sort_constructors_first
將建構子宣告排序在其他成員之前。
sort_pub_dependencies
依字母順序排序 pub 相依性。
sort_unnamed_constructors_first
將未命名的建構子宣告排序在最前面。
specify_nonobvious_local_variable_types
(實驗性)
為不明顯的區域變數指定型別註解。
specify_nonobvious_property_types
(未發布)
為不明顯的頂層和靜態變數指定型別註解。
super_goes_last
(已移除)
在建構子初始化列表中,將 super
呼叫放在最後。
test_types_in_equals
在 operator ==(Object other)
中測試引數的型別。
throw_in_finally
避免在 finally
區塊中使用 throw
。
tighten_type_of_initializing_formals
收緊初始化形式參數的型別。
type_annotate_public_apis
為公開 API 添加型別註解。
type_init_formals
不要為初始化形式參數添加型別註解。
type_literal_in_constant_pattern
不要在常數模式中使用型別字面值。
unawaited_futures
async
函式主體中的 Future
結果必須使用 await
等待或使用 dart:async
標記為 unawaited
。
unintended_html_in_doc_comment
Markdown 會將文件註解中的角括號視為 HTML。
unnecessary_await_in_return
在 return 中不必要的 await
關鍵字。
unnecessary_brace_in_string_interps
在不需要時,避免在插值中使用大括號。
unnecessary_breaks
當暗示有 break 時,不要使用明確的 break
。
unnecessary_const
避免使用 const
關鍵字。
unnecessary_constructor_name
不必要的 .new
建構子名稱。
unnecessary_final
不要對區域變數使用 final
。
unnecessary_getters_setters
避免僅為了「安全」而將欄位包裝在 getter 和 setter 中。
unnecessary_lambdas
當可以使用 tear-off 時,不要建立 lambda。
unnecessary_late
在不需要時,不要指定 late
修飾符。
unnecessary_library_directive
除非有文件註解或註釋,否則避免使用程式庫指令。
unnecessary_library_name
在 library
宣告中不要有程式庫名稱。
unnecessary_new
不必要的 new 關鍵字。
unnecessary_null_aware_assignments
避免在可空感知賦值中使用 null
。
unnecessary_null_aware_operator_on_extension_on_nullable
在可空型別的擴展上,不必要的可空感知運算子。
unnecessary_null_checks
(實驗性)
不必要的 null
檢查。
unnecessary_null_in_if_null_operators
避免在 ??
運算子中使用 null
。
unnecessary_nullable_for_final_variable_declarations
對於以非可空值初始化的 final 變數,使用非可空型別。
unnecessary_overrides
不要覆寫方法以使用相同的參數執行 super 方法呼叫。
unnecessary_parenthesis
可以移除不必要的括號。
unnecessary_raw_strings
不必要的原始字串。
unnecessary_statements
避免使用不必要的語句。
unnecessary_string_escapes
移除字串中不必要的反斜線。
unnecessary_string_interpolations
不必要的字串插值。
unnecessary_this
除非為了避免遮蔽,否則不要使用 this
存取成員。
unnecessary_to_list_in_spreads
展開運算子中不必要的 toList()
。
unreachable_from_main
可執行程式庫中無法存取的頂層成員。
unrelated_type_equality_checks
使用不相關型別的參考來呼叫相等運算子 ==
。
unsafe_html
(已移除)
避免使用不安全的 HTML API。
unsafe_variance
(未發布)
不安全的型別:在非協變位置中有型別變數。
use_build_context_synchronously
不要在非同步間隙中使用 BuildContext
。
use_colored_box
使用 ColoredBox
。
use_decorated_box
使用 DecoratedBox
。
use_enums
使用列舉,而不是行為類似列舉的類別。
use_full_hex_values_for_flutter_colors
偏好使用 8 位十六進位整數 (例如,0xFFFFFFFF) 來實例化 Color。
use_function_type_syntax_for_parameters
為參數使用泛型函式型別語法。
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。
use_late_for_private_fields_and_variables
(實驗性)
為具有非可空型別的私有成員使用 late。
use_named_constants
使用預定義的具名常數。
use_raw_strings
使用原始字串來避免跳脫字元。
use_rethrow_when_possible
使用 rethrow 來重新拋出捕獲的例外。
use_setters_to_change_properties
對於概念上會更改屬性的操作,使用 setter。
use_string_buffers
使用字串緩衝區來組合字串。
use_string_in_part_of_directives
在 part of 指令中使用字串。
use_super_parameters
(實驗性)
盡可能使用超級初始化器參數。
use_test_throws_matchers
使用 throwsA 匹配器而不是 fail()。
use_to_and_as_if_applicable
如果適用,方法名稱請以 to/_to 或 as/_as 開頭。
use_truncating_division
使用截斷除法。
valid_regexps
使用有效的正規表示式語法。
void_checks
不要賦值給 void
。
除非另有說明,否則本網站上的文件反映 Dart 3.6.0。頁面最後更新於 2024-11-18。 檢視原始碼 或 回報問題。