跳到主要內容

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.8.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 子句的捕獲塊。

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

避免使用型別作為引數名。

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

僅 await Future。

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

不要將可空值轉換為不可空型別。

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_member_use_from_same_package

避免在宣告它們的包中使用已棄用的元素。

build
diagnostic_describe_all_properties

在除錯方法中引用所有公共屬性。

build
directives_ordering

遵守高效 Dart 指南的指令排序約定。

build
discarded_futures

同步函式中不應有返回 Future 的呼叫,除非它們被賦值或返回。

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

使用健全的空安全。

error
eol_at_end_of_file

在檔案末尾放置一個換行符。

build
exhaustive_cases

為類似列舉的類中的所有常量定義 case 子句。

build thumb_up flutter
file_names

使用 lowercase_with_underscores 命名原始檔。

circles thumb_up flutter
flutter_style_todos

使用 Flutter TODO 格式:// TODO(使用者名稱): 訊息, https://問題連結。

build
hash_and_equals

如果重寫 ==,則始終重寫 hashCode

build circles thumb_up flutter
implementation_imports

不要從另一個包匯入實現檔案。

thumb_up flutter
implicit_call_tearoffs

將物件用作 Function 時,顯式分離 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

使用匹配的超引數名稱。

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

無操作原始操作。

build
null_check_on_nullable_type_parameter

不要對可能為空的型別引數使用 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

首選將斷言放在初始化列表中。

prefer_asserts_with_message

首選帶訊息的斷言。

prefer_bool_in_asserts

首選使用布林值作為斷言條件。

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

在 @immutable 類上,建構函式的引數首選 const 字面量。

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

對於主體是單個返回語句的簡短成員使用 =>。

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

從可迭代物件構建對映時首選 for 元素。

build thumb_up flutter
prefer_foreach

僅使用 forEach 將函式應用於所有元素。

build
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

首選 int 字面量而不是 double 字面量。

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

首選空感知方法呼叫。

prefer_null_aware_operators

首選使用空感知運算子。

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

不要使用 Null 型別,除非您確定不想要 void。

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 中使用安全 URL。

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

在 widget 例項建立中,將子屬性放在最後排序。

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 circles thumb_up flutter
super_goes_last

super 呼叫放在建構函式初始化列表的最後。

error
switch_on_type

避免對 'Type' 進行 switch 語句。

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

不要忽略未產生的診斷程式碼。

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

對於用非空值初始化的 final 變數,使用非空型別。

build thumb_up flutter
unnecessary_overrides

不要重寫方法以使用相同引數呼叫父方法。

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_unawaited

不必要的 'unawaited' 使用。

build
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

例項化 Color 時首選 8 位十六進位制整數(例如 0xFFFFFFFF)。

build flutter
use_function_type_syntax_for_parameters

引數使用泛型函式型別語法。

build thumb_up flutter
use_if_null_to_convert_nulls_to_bools

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

build
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_null_aware_elements

測試 null 的 if 元素可以用空感知元素替換。

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

儘可能使用超初始化器引數。

science build thumb_up flutter
use_test_throws_matchers

使用 throwsA 匹配器而不是 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