跳到主要内容

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 集的超集,后者部分决定了上传到 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

避免私有 typedef 函数。

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

避免在 setters 上使用返回类型。

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

避免没有 getters 的 setters。

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

不要将可空值强制转换为不可空类型。

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

在 debug 方法中引用所有公共属性。

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

请使用健全的空安全。

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://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

没有 default 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

首选将 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

从 iterables 构建 maps 时,首选 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

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

build
prefer_interpolation_to_compose_strings

使用插值来组合字符串和值。

build thumb_up flutter
prefer_is_empty

对于 Iterables 和 Maps,使用 isEmpty

build circles thumb_up flutter
prefer_is_not_empty

对于 Iterables 和 Maps,使用 isNotEmpty

build circles thumb_up flutter
prefer_is_not_operator

首选 is! 运算符。

build thumb_up flutter
prefer_iterable_wheretype

首选在 iterable 上使用 whereType

build circles thumb_up flutter
prefer_mixin

首选使用 mixins。

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

尽可能使用 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 中使用安全 urls。

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 实例创建中,将 child 属性排在最后。

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

返回语句中不必要的 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

对于用非空值初始化的 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_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

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

尽可能使用超类初始化参数。

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