目錄

Use the Dart linter to identify possible problems in your Dart code. You can use the linter through your IDE or with the dart analyze command. For information on how to enable and disable individual linter rules, see individual rules sections of the analyzer documentation.

This page lists all the linter rules, with details such as when you might want to use each rule, what code patterns trigger it, and how you might fix your code.

Sets

To avoid the need to individually select compatible linter rules, consider starting with a linter rule set, which the following packages provide:

lints
Contains two rule sets curated by the Dart team. We recommend using at least the core rule set, which is used when scoring packages uploaded to pub.dev. Or, better yet, use the recommended rule set, a superset of core that identifies additional issues and enforces style and format. If you’re writing Flutter code, use the rule set in the flutter_lints package, which builds on lints.
flutter_lints
Contains the flutter rule set, which the Flutter team encourages you to use in Flutter apps, packages, and plugins. This rule set is a superset of the recommended set, which is itself a superset of the core set that partially determines the score of packages uploaded to pub.dev.

To learn how to use a specific rule set, visit the documentation for enabling and disabling linter rules.

To find more predefined rule sets, check out the #lints topic on pub.dev.

Types

Each rule belongs to one of the following groups:

Errors
Possible errors or mistakes in your code.
Style
Matters of style, largely derived from the Dart style guide.
Pub
Possible issues with pub package setup.

Status

Each rule has a status or maturity level:

Stable
These rules are safe to use and are verified as functional with the latest versions of the Dart language. All rules are considered stable unless they’re marked as experimental, deprecated, or removed.
Experimental
These rules are still under evaluation and might never be stabilized. Use these with caution and report any issues you come across.
Deprecated
These rules are no longer suggested for use and might be removed in a future Dart release.
Removed
These rules have been already been removed in the latest stable Dart release.

Quick fixes

Some rules can be fixed automatically using quick fixes. A quick fix is an automated edit targeted at fixing the issue reported by the linter rule.

If the rule has a quick fix, it can be applied using dart fix or using your editor with Dart support. To learn more, see Quick fixes for analysis issues.

Rules

The following is an index of all linter rules and a short description of their functionality. To learn more about each rule, click on its name.

For an auto-generated list containing all linter rules in Dart 3.0.7, check out All linter rules.

Error rules

These rules identify possible errors and other mistakes in your code.

always_use_package_imports
Has a quick fix
Avoid relative imports for files in lib/.

avoid_dynamic_calls
Avoid method calls or property accesses on a “dynamic” target.

avoid_empty_else
core rule set recommended rule set flutter rule set Has a quick fix
Avoid empty else statements.

avoid_print
flutter rule set Has a quick fix
Avoid print calls in production code.

avoid_relative_lib_imports
core rule set recommended rule set flutter rule set Has a quick fix
Avoid relative imports for files in lib/.

avoid_returning_null_for_future (Deprecated)
Has a quick fix
Avoid returning null for Future.

avoid_slow_async_io
Avoid slow async dart:io methods.

avoid_type_to_string
Avoid .toString() in production code since results may be minified.

avoid_types_as_parameter_names
core rule set recommended rule set flutter rule set Has a quick fix
Avoid types as parameter names.

avoid_web_libraries_in_flutter
flutter rule set
Avoid using web-only libraries outside Flutter web plugin packages.

cancel_subscriptions
Cancel instances of dart.async.StreamSubscription.

close_sinks
Close instances of dart.core.Sink.

collection_methods_unrelated_type
Invocation of various collection methods with arguments of unrelated types.

comment_references
Only reference in scope identifiers in doc comments.

control_flow_in_finally
recommended rule set flutter rule set
Avoid control flow in finally blocks.

deprecated_member_use_from_same_package
Avoid using deprecated elements from within the package in which they are declared.

diagnostic_describe_all_properties
Has a quick fix
DO reference all public properties in debug methods.

discarded_futures
Has a quick fix
Don’t invoke asynchronous functions in non-async blocks.

empty_statements
recommended rule set flutter rule set Has a quick fix
Avoid empty statements.

hash_and_equals
core rule set recommended rule set flutter rule set Has a quick fix
Always override hashCode if overriding ==.

implicit_reopen (Experimental)
Has a quick fix
Don’t implicitly reopen classes.

invalid_case_patterns (Experimental)
Has a quick fix
Use case expressions that are valid in Dart 3.0.

invariant_booleans (Removed)
Conditions should not unconditionally evaluate to true or to false.

iterable_contains_unrelated_type (Deprecated)
core rule set recommended rule set flutter rule set
Invocation of Iterable.contains with references of unrelated types.

list_remove_unrelated_type (Deprecated)
core rule set recommended rule set flutter rule set
Invocation of remove with references of unrelated types.

literal_only_boolean_expressions
Boolean expression composed only with literals.

no_adjacent_strings_in_list
Don’t use adjacent strings in list.

no_duplicate_case_values
core rule set recommended rule set flutter rule set Has a quick fix
Don’t use more than one case with same value.

no_logic_in_create_state
flutter rule set
Don’t put any logic in createState.

no_self_assignments (Unreleased)
Don’t assign a variable to itself.

no_wildcard_variable_uses (Unreleased)
Don’t use wildcard parameters or variables.

prefer_relative_imports
Has a quick fix
Prefer relative imports for files in lib/.

prefer_void_to_null
recommended rule set flutter rule set Has a quick fix
Don’t use the Null type, unless you are positive that you don’t want void.

test_types_in_equals
Test type arguments in operator ==(Object other).

throw_in_finally
Avoid throw in finally block.

unnecessary_statements
Avoid using unnecessary statements.

unrelated_type_equality_checks
core rule set recommended rule set flutter rule set
Equality operator == invocation with references of unrelated types.

unsafe_html
Avoid unsafe HTML APIs.

use_build_context_synchronously (Experimental)
flutter rule set
Do not use BuildContexts across async gaps.

use_key_in_widget_constructors
flutter rule set Has a quick fix
Use key in widget constructors.

valid_regexps
core rule set recommended rule set flutter rule set
Use valid regular expression syntax.

Style rules

These rules identify opportunities for style improvements, largely derived from the Dart style guide.

always_declare_return_types
Has a quick fix
Declare method return types.

always_put_control_body_on_new_line
Has a quick fix
Separate the control structure expression from its statement.

always_put_required_named_parameters_first
Has a quick fix
Put required named parameters first.

always_require_non_null_named_parameters (Deprecated)
Has a quick fix
Specify @required on named parameters without defaults.

always_specify_types
Has a quick fix
Specify type annotations.

annotate_overrides
recommended rule set flutter rule set Has a quick fix
Annotate overridden members.

avoid_annotating_with_dynamic
Has a quick fix
Avoid annotating with dynamic when not required.

avoid_as (Removed)
Avoid using as.

avoid_bool_literals_in_conditional_expressions
Avoid bool literals in conditional expressions.

avoid_catches_without_on_clauses
Avoid catches without on clauses.

avoid_catching_errors
Don’t explicitly catch Error or types that implement it.

avoid_classes_with_only_static_members
Avoid defining a class that contains only static members.

avoid_double_and_int_checks
Avoid double and int checks.

avoid_equals_and_hash_code_on_mutable_classes
Avoid overloading operator == and hashCode on classes not marked @immutable.

avoid_escaping_inner_quotes
Has a quick fix
Avoid escaping inner quotes by converting surrounding quotes.

avoid_field_initializers_in_const_classes
Avoid field initializers in const classes.

avoid_final_parameters
Avoid final for parameter declarations.

avoid_function_literals_in_foreach_calls
recommended rule set flutter rule set Has a quick fix
Avoid using forEach with a function literal.

avoid_implementing_value_types
Don’t implement classes that override ==.

avoid_init_to_null
recommended rule set flutter rule set Has a quick fix
Don’t explicitly initialize variables to null.

avoid_js_rounded_ints
Avoid JavaScript rounded ints.

avoid_multiple_declarations_per_line
Has a quick fix
Don’t declare multiple variables on a single line.

avoid_null_checks_in_equality_operators
recommended rule set flutter rule set Has a quick fix
Don’t check for null in custom == operators.

avoid_positional_boolean_parameters
Avoid positional boolean parameters.

avoid_private_typedef_functions
Has a quick fix
Avoid private typedef functions.

avoid_redundant_argument_values
Has a quick fix
Avoid redundant argument values.

avoid_renaming_method_parameters
recommended rule set flutter rule set Has a quick fix
Don’t rename parameters of overridden methods.

avoid_return_types_on_setters
recommended rule set flutter rule set Has a quick fix
Avoid return types on setters.

avoid_returning_null (Deprecated)
Avoid returning null from members whose return type is bool, double, int, or num.

avoid_returning_null_for_void
recommended rule set flutter rule set Has a quick fix
Avoid returning null for void.

avoid_returning_this
Avoid returning this from methods just to enable a fluent interface.

avoid_setters_without_getters
Avoid setters without getters.

avoid_shadowing_type_parameters
core rule set recommended rule set flutter rule set
Avoid shadowing type parameters.

avoid_single_cascade_in_expression_statements
recommended rule set flutter rule set Has a quick fix
Avoid single cascade in expression statements.

avoid_types_on_closure_parameters
Has a quick fix
Avoid annotating types for function expression parameters.

avoid_unnecessary_containers
flutter rule set Has a quick fix
Avoid unnecessary containers.

avoid_unused_constructor_parameters
Has a quick fix
Avoid defining unused parameters in constructors.

avoid_void_async
Has a quick fix
Avoid async functions that return void.

await_only_futures
core rule set recommended rule set flutter rule set Has a quick fix
Await only futures.

camel_case_extensions
core rule set recommended rule set flutter rule set
Name extensions using UpperCamelCase.

camel_case_types
core rule set recommended rule set flutter rule set
Name types using UpperCamelCase.

cascade_invocations
Has a quick fix
Cascade consecutive method invocations on the same reference.

cast_nullable_to_non_nullable
Has a quick fix
Don’t cast a nullable value to a non nullable type.

combinators_ordering
Has a quick fix
Sort combinator names alphabetically.

conditional_uri_does_not_exist
Missing conditional import.

constant_identifier_names
recommended rule set flutter rule set Has a quick fix
Prefer using lowerCamelCase for constant names.

curly_braces_in_flow_control_structures
core rule set recommended rule set flutter rule set Has a quick fix
DO use curly braces for all flow control structures.

dangling_library_doc_comments
Has a quick fix
Attach library doc comments to library directives.

deprecated_consistency
Missing deprecated annotation.

directives_ordering
Has a quick fix
Adhere to Effective Dart Guide directives sorting conventions.

do_not_use_environment
Do not use environment declared variables.

empty_catches
core rule set recommended rule set flutter rule set Has a quick fix
Avoid empty catch blocks.

empty_constructor_bodies
recommended rule set flutter rule set Has a quick fix
Use ; instead of {} for empty constructor bodies.

enable_null_safety (Removed)
Do use sound null safety.

eol_at_end_of_file
Has a quick fix
Put a single newline at end of file.

exhaustive_cases
recommended rule set flutter rule set Has a quick fix
Define case clauses for all constants in enum-like classes.

file_names
core rule set recommended rule set flutter rule set
Name source files using lowercase_with_underscores.

flutter_style_todos
Use Flutter TODO format: // TODO(username): message, https://URL-to-issue.

implementation_imports
recommended rule set flutter rule set
Don’t import implementation files from another package.

implicit_call_tearoffs
Has a quick fix
Explicitly tear-off call methods when using an object as a Function.

join_return_with_assignment
Join return statement with assignment when possible.

leading_newlines_in_multiline_strings
Has a quick fix
Start multiline strings with a newline.

library_annotations
Has a quick fix
Attach library annotations to library directives.

library_names
recommended rule set flutter rule set
Name libraries using lowercase_with_underscores.

library_prefixes
recommended rule set flutter rule set
Use lowercase_with_underscores when specifying a library prefix.

library_private_types_in_public_api
recommended rule set flutter rule set
Avoid using private types in public APIs.

lines_longer_than_80_chars
Avoid lines longer than 80 characters.

matching_super_parameters (Unreleased)
Use matching super parameter names.

missing_whitespace_between_adjacent_strings
Missing whitespace between adjacent strings.

no_default_cases (Experimental)
No default cases.

no_leading_underscores_for_library_prefixes
recommended rule set flutter rule set Has a quick fix
Avoid leading underscores for library prefixes.

no_leading_underscores_for_local_identifiers
recommended rule set flutter rule set Has a quick fix
Avoid leading underscores for local identifiers.

no_literal_bool_comparisons (Unreleased)
Has a quick fix
Don’t compare booleans to boolean literals.

no_runtimeType_toString
Avoid calling toString() on runtimeType.

non_constant_identifier_names
core rule set recommended rule set flutter rule set Has a quick fix
Name non-constant identifiers using lowerCamelCase.

noop_primitive_operations
Has a quick fix
Noop primitive operations.

null_check_on_nullable_type_parameter
core rule set recommended rule set flutter rule set Has a quick fix
Don’t use null check on a potentially nullable type parameter.

null_closures
recommended rule set flutter rule set Has a quick fix
Do not pass null as an argument where a closure is expected.

omit_local_variable_types
Has a quick fix
Omit type annotations for local variables.

one_member_abstracts
Avoid defining a one-member abstract class when a simple function will do.

only_throw_errors
Only throw instances of classes extending either Exception or Error.

overridden_fields
recommended rule set flutter rule set
Don’t override fields.

package_api_docs
Provide doc comments for all public APIs.

package_prefixed_library_names
core rule set recommended rule set flutter rule set
Prefix library names with the package name and a dot-separated path.

parameter_assignments
Don’t reassign references to parameters of functions or methods.

prefer_adjacent_string_concatenation
recommended rule set flutter rule set Has a quick fix
Use adjacent strings to concatenate string literals.

prefer_asserts_in_initializer_lists
Prefer putting asserts in initializer lists.

prefer_asserts_with_message
Prefer asserts with message.

prefer_bool_in_asserts (Removed)
Prefer using a boolean as the assert condition.

prefer_collection_literals
recommended rule set flutter rule set Has a quick fix
Use collection literals when possible.

prefer_conditional_assignment
recommended rule set flutter rule set Has a quick fix
Prefer using ??= over testing for null.

prefer_const_constructors
flutter rule set Has a quick fix
Prefer const with constant constructors.

prefer_const_constructors_in_immutables
flutter rule set Has a quick fix
Prefer declaring const constructors on @immutable classes.

prefer_const_declarations
flutter rule set Has a quick fix
Prefer const over final for declarations.

prefer_const_literals_to_create_immutables
flutter rule set Has a quick fix
Prefer const literals as parameters of constructors on @immutable classes.

prefer_constructors_over_static_methods
Prefer defining constructors instead of static methods to create instances.

prefer_contains
recommended rule set flutter rule set Has a quick fix
Use contains for List and String instances.

prefer_double_quotes
Has a quick fix
Prefer double quotes where they won’t require escape sequences.

prefer_equal_for_default_values (Removed)
recommended rule set flutter rule set
Use = to separate a named parameter from its default value.

prefer_expression_function_bodies
Has a quick fix
Use => for short members whose body is a single return statement.

prefer_final_fields
recommended rule set flutter rule set Has a quick fix
Private field could be final.

prefer_final_in_for_each
Has a quick fix
Prefer final in for-each loop variable if reference is not reassigned.

prefer_final_locals
Has a quick fix
Prefer final for variable declarations if they are not reassigned.

prefer_final_parameters
Has a quick fix
Prefer final for parameter declarations if they are not reassigned.

prefer_for_elements_to_map_fromIterable
recommended rule set flutter rule set Has a quick fix
Prefer ‘for’ elements when building maps from iterables.

prefer_foreach
Use forEach to only apply a function to all the elements.

prefer_function_declarations_over_variables
recommended rule set flutter rule set Has a quick fix
Use a function declaration to bind a function to a name.

prefer_generic_function_type_aliases
core rule set recommended rule set flutter rule set Has a quick fix
Prefer generic function type aliases.

prefer_if_elements_to_conditional_expressions
Has a quick fix
Prefer if elements to conditional expressions where possible.

prefer_if_null_operators
recommended rule set flutter rule set Has a quick fix
Prefer using if null operators.

prefer_initializing_formals
recommended rule set flutter rule set Has a quick fix
Use initializing formals when possible.

prefer_inlined_adds
recommended rule set flutter rule set Has a quick fix
Inline list item declarations where possible.

prefer_int_literals
Has a quick fix
Prefer int literals over double literals.

prefer_interpolation_to_compose_strings
recommended rule set flutter rule set Has a quick fix
Use interpolation to compose strings and values.

prefer_is_empty
core rule set recommended rule set flutter rule set Has a quick fix
Use isEmpty for Iterables and Maps.

prefer_is_not_empty
core rule set recommended rule set flutter rule set Has a quick fix
Use isNotEmpty for Iterables and Maps.

prefer_is_not_operator
recommended rule set flutter rule set Has a quick fix
Prefer is! operator.

prefer_iterable_whereType
core rule set recommended rule set flutter rule set Has a quick fix
Prefer to use whereType on iterable.

prefer_mixin
Prefer using mixins.

prefer_null_aware_method_calls
Prefer null aware method calls.

prefer_null_aware_operators
recommended rule set flutter rule set Has a quick fix
Prefer using null aware operators.

prefer_single_quotes
Has a quick fix
Only use double quotes for strings containing single quotes.

prefer_spread_collections
recommended rule set flutter rule set Has a quick fix
Use spread collections when possible.

prefer_typing_uninitialized_variables
core rule set recommended rule set flutter rule set Has a quick fix
Prefer typing uninitialized variables and fields.

provide_deprecation_message
core rule set recommended rule set flutter rule set
Provide a deprecation message, via @Deprecated(“message”).

public_member_api_docs
Document all public members.

recursive_getters
recommended rule set flutter rule set
Property getter recursively returns itself.

require_trailing_commas
Has a quick fix
Use trailing commas for all function calls and declarations.

sized_box_for_whitespace
flutter rule set Has a quick fix
SizedBox for whitespace.

sized_box_shrink_expand
Use SizedBox shrink and expand named constructors.

slash_for_doc_comments
recommended rule set flutter rule set Has a quick fix
Prefer using /// for doc comments.

sort_child_properties_last
flutter rule set Has a quick fix
Sort child properties last in widget instance creations.

sort_constructors_first
Has a quick fix
Sort constructor declarations before other members.

sort_unnamed_constructors_first
Has a quick fix
Sort unnamed constructor declarations first.

super_goes_last (Removed)
Place the super call last in a constructor initialization list.

tighten_type_of_initializing_formals
Tighten type of initializing formal.

type_annotate_public_apis
Has a quick fix
Type annotate public APIs.

type_init_formals
recommended rule set flutter rule set Has a quick fix
Don’t type annotate initializing formals.

type_literal_in_constant_pattern
Has a quick fix
Don’t use constant patterns with type literals.

unawaited_futures
Has a quick fix
Future results in async function bodies must be awaited or marked unawaited using dart:async.

unnecessary_await_in_return
Unnecessary await keyword in return.

unnecessary_brace_in_string_interps
recommended rule set flutter rule set Has a quick fix
Avoid using braces in interpolation when not needed.

unnecessary_breaks
Has a quick fix
Don’t use explicit breaks when a break is implied.

unnecessary_const
recommended rule set flutter rule set Has a quick fix
Avoid const keyword.

unnecessary_constructor_name
recommended rule set flutter rule set Has a quick fix
Unnecessary .new constructor name.

unnecessary_final
Has a quick fix
Don’t use final for local variables.

unnecessary_getters_setters
recommended rule set flutter rule set Has a quick fix
Avoid wrapping fields in getters and setters just to be “safe”.

unnecessary_lambdas
Has a quick fix
Don’t create a lambda when a tear-off will do.

unnecessary_late
recommended rule set flutter rule set Has a quick fix
Don’t specify the late modifier when it is not needed.

unnecessary_library_directive
Has a quick fix
Avoid library directives unless they have documentation comments or annotations.

unnecessary_new
recommended rule set flutter rule set Has a quick fix
Unnecessary new keyword.

unnecessary_null_aware_assignments
recommended rule set flutter rule set Has a quick fix
Avoid null in null-aware assignment.

unnecessary_null_aware_operator_on_extension_on_nullable
Unnecessary null aware operator on extension on a nullable type.

unnecessary_null_checks (Experimental)
Has a quick fix
Unnecessary null checks.

unnecessary_null_in_if_null_operators
recommended rule set flutter rule set Has a quick fix
Avoid using null in if null operators.

unnecessary_nullable_for_final_variable_declarations
recommended rule set flutter rule set Has a quick fix
Use a non-nullable type for a final variable initialized with a non-nullable value.

unnecessary_overrides
core rule set recommended rule set flutter rule set Has a quick fix
Don’t override a method to do a super method invocation with the same parameters.

unnecessary_parenthesis
Has a quick fix
Unnecessary parentheses can be removed.

unnecessary_raw_strings
Has a quick fix
Unnecessary raw string.

unnecessary_string_escapes
recommended rule set flutter rule set Has a quick fix
Remove unnecessary backslashes in strings.

unnecessary_string_interpolations
recommended rule set flutter rule set Has a quick fix
Unnecessary string interpolation.

unnecessary_this
recommended rule set flutter rule set Has a quick fix
Don’t access members with this unless avoiding shadowing.

unnecessary_to_list_in_spreads
Has a quick fix
Unnecessary toList() in spreads.

unreachable_from_main
Unreachable top-level members in executable libraries.

use_colored_box
Use ColoredBox.

use_decorated_box
Has a quick fix
Use DecoratedBox.

use_enums
Has a quick fix
Use enums rather than classes that behave like enums.

use_full_hex_values_for_flutter_colors
flutter rule set Has a quick fix
Prefer an 8-digit hexadecimal integer(0xFFFFFFFF) to instantiate Color.

use_function_type_syntax_for_parameters
recommended rule set flutter rule set Has a quick fix
Use generic function type syntax for parameters.

use_if_null_to_convert_nulls_to_bools
Use if-null operators to convert nulls to bools.

use_is_even_rather_than_modulo
Prefer intValue.isOdd/isEven instead of checking the result of % 2.

use_late_for_private_fields_and_variables (Experimental)
Use late for private members with a non-nullable type.

use_named_constants
Use predefined named constants.

use_raw_strings
Has a quick fix
Use raw string to avoid escapes.

use_rethrow_when_possible
recommended rule set flutter rule set Has a quick fix
Use rethrow to rethrow a caught exception.

use_setters_to_change_properties
Use a setter for operations that conceptually change a property.

use_string_buffers
Use string buffers to compose strings.

use_string_in_part_of_directives
Has a quick fix
Use string in part of directives.

use_super_parameters (Experimental)
Has a quick fix
Use super-initializer parameters where possible.

use_test_throws_matchers
Use throwsA matcher instead of fail().

use_to_and_as_if_applicable
Start the name of the method with to/_to or as/_as if applicable.

void_checks
core rule set recommended rule set flutter rule set
Don’t assign to void.

Pub rules

These rules identify possible issues around pub package setup.

depend_on_referenced_packages
core rule set recommended rule set flutter rule set
Depend on referenced packages.

package_names
recommended rule set flutter rule set
Use lowercase_with_underscores for package names.

secure_pubspec_urls
Use secure urls in pubspec.yaml.

sort_pub_dependencies
Sort pub dependencies alphabetically.