跳到主要内容

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