Linter rules
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 therecommended
rule set, a superset ofcore
that identifies additional issues and enforces style and format. If you're writing Flutter code, use the rule set in theflutter_lints
package, which builds onlints
.
- 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 therecommended
set, which is itself a superset of thecore
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.
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 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 a specific rule, click the Learn more button on its card.
For an auto-generated list containing all linter rules in Dart 3.6.0
, check out All linter rules.
always_put_control_body_on_new_line
Separate the control structure expression from its statement.
always_require_non_null_named_parameters
Specify @required
on named parameters without defaults.
avoid_bool_literals_in_conditional_expressions
Avoid bool
literals in conditional expressions.
avoid_classes_with_only_static_members
Avoid defining a class that contains only static members.
avoid_equals_and_hash_code_on_mutable_classes
Avoid overloading operator == and hashCode on classes not marked @immutable
.
avoid_escaping_inner_quotes
Avoid escaping inner quotes by converting surrounding quotes.
avoid_function_literals_in_foreach_calls
Avoid using forEach
with a function literal.
avoid_multiple_declarations_per_line
Don't declare multiple variables on a single line.
avoid_relative_lib_imports
Avoid relative imports for files in lib/
.
avoid_renaming_method_parameters
Don't rename parameters of overridden methods.
avoid_returning_null
Avoid returning null from members whose return type is bool, double, int, or num.
avoid_single_cascade_in_expression_statements
Avoid single cascade in expression statements.
avoid_types_as_parameter_names
Avoid types as parameter names.
avoid_types_on_closure_parameters
Avoid annotating types for function expression parameters.
avoid_unstable_final_fields
Avoid overriding a final field to return different values if called multiple times.
avoid_web_libraries_in_flutter
Avoid using web-only libraries outside Flutter web plugin packages.
constant_identifier_names
Prefer using lowerCamelCase for constant names.
curly_braces_in_flow_control_structures
DO use curly braces for all flow control structures.
dangling_library_doc_comments
Attach library doc comments to library directives.
deprecated_member_use_from_same_package
Avoid using deprecated elements from within the package in which they are declared.
diagnostic_describe_all_properties
DO reference all public properties in debug methods.
empty_constructor_bodies
Use ;
instead of {}
for empty constructor bodies.
exhaustive_cases
Define case clauses for all constants in enum-like classes.
flutter_style_todos
Use Flutter TODO format: // TODO(username): message, https://URL-to-issue.
implementation_imports
Don't import implementation files from another package.
implicit_call_tearoffs
Explicitly tear-off call
methods when using an object as a Function.
invalid_runtime_check_with_js_interop_types
Avoid runtime type tests with JS interop types where the result may not be platform-consistent.
invariant_booleans
Conditions should not unconditionally evaluate to true
or to false
.
library_annotations
Attach library annotations to library directives.
library_prefixes
Use lowercase_with_underscores
when specifying a library prefix.
library_private_types_in_public_api
Avoid using private types in public APIs.
no_duplicate_case_values
Don't use more than one case with same value.
no_leading_underscores_for_library_prefixes
Avoid leading underscores for library prefixes.
no_leading_underscores_for_local_identifiers
Avoid leading underscores for local identifiers.
no_wildcard_variable_uses
Don't use wildcard parameters or variables.
non_constant_identifier_names
Name non-constant identifiers using lowerCamelCase.
null_check_on_nullable_type_parameter
Don't use null
check on a potentially nullable type parameter.
null_closures
Do not pass null
as an argument where a closure is expected.
omit_obvious_local_variable_types
Omit obvious type annotations for local variables.
omit_obvious_property_types
Omit obvious type annotations for top-level and static variables.
one_member_abstracts
Avoid defining a one-member abstract class when a simple function will do.
package_prefixed_library_names
Prefix library names with the package name and a dot-separated path.
prefer_adjacent_string_concatenation
Use adjacent strings to concatenate string literals.
prefer_conditional_assignment
Prefer using ??=
over testing for null
.
prefer_const_constructors_in_immutables
Prefer declaring const
constructors on @immutable
classes.
prefer_const_literals_to_create_immutables
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_equal_for_default_values
Use =
to separate a named parameter from its default value.
prefer_expression_function_bodies
Use => for short members whose body is a single return statement.
prefer_final_in_for_each
Prefer final in for-each loop variable if reference is not reassigned.
prefer_final_locals
Prefer final for variable declarations if they are not reassigned.
prefer_final_parameters
Prefer final for parameter declarations if they are not reassigned.
prefer_for_elements_to_map_fromIterable
Prefer for
elements when building maps from iterables.
prefer_function_declarations_over_variables
Use a function declaration to bind a function to a name.
prefer_generic_function_type_aliases
Prefer generic function type aliases.
prefer_if_elements_to_conditional_expressions
Prefer if elements to conditional expressions where possible.
prefer_interpolation_to_compose_strings
Use interpolation to compose strings and values.
prefer_iterable_whereType
Prefer to use whereType
on iterable.
prefer_typing_uninitialized_variables
Prefer typing uninitialized variables and fields.
prefer_void_to_null
Don't use the Null type, unless you are positive that you don't want void.
provide_deprecation_message
Provide a deprecation message, via @Deprecated("message")
.
require_trailing_commas
Use trailing commas for all parameter lists and argument lists.
sort_child_properties_last
Sort child properties last in widget instance creations.
specify_nonobvious_local_variable_types
Specify non-obvious type annotations for local variables.
specify_nonobvious_property_types
Specify non-obvious type annotations for top-level and static variables.
type_literal_in_constant_pattern
Don't use constant patterns with type literals.
unawaited_futures
Future
results in async
function bodies must be await
ed or marked unawaited
using dart:async
.
unintended_html_in_doc_comment
Use of angle brackets in a doc comment is treated as HTML by Markdown.
unnecessary_brace_in_string_interps
Avoid using braces in interpolation when not needed.
unnecessary_getters_setters
Avoid wrapping fields in getters and setters just to be "safe".
unnecessary_late
Don't specify the late
modifier when it is not needed.
unnecessary_library_directive
Avoid library directives unless they have documentation comments or annotations.
unnecessary_library_name
Don't have a library name in a library
declaration.
unnecessary_null_aware_assignments
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_in_if_null_operators
Avoid using null
in ??
operators.
unnecessary_nullable_for_final_variable_declarations
Use a non-nullable type for a final variable initialized with a non-nullable value.
unnecessary_overrides
Don't override a method to do a super method invocation with the same parameters.
unnecessary_string_escapes
Remove unnecessary backslashes in strings.
unnecessary_this
Don't access members with this
unless avoiding shadowing.
use_full_hex_values_for_flutter_colors
Prefer an 8-digit hexadecimal integer (for example, 0xFFFFFFFF) to instantiate a Color.
use_function_type_syntax_for_parameters
Use generic function type syntax for parameters.
use_is_even_rather_than_modulo
Prefer intValue.isOdd/isEven instead of checking the result of % 2.
use_late_for_private_fields_and_variables
Use late for private members with a non-nullable type.
use_setters_to_change_properties
Use a setter for operations that conceptually change a property.
use_string_in_part_of_directives
Use string in part of directives.
use_super_parameters
Use super-initializer parameters where possible.
use_to_and_as_if_applicable
Start the name of the method with to/_to or as/_as if applicable.
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2025-01-04. View source or report an issue.