Skip to main content

Linter rules

Details about the Dart linter and its style rules you can choose.

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.

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.14.0, check out All linter rules.


Declare method return types.

build
Learn more

Separate the control structure expression from its statement.

build
Learn more

Put required named parameters first.

build
Learn more

Specify @required on named parameters without defaults.

error
Learn more

Specify type annotations.

build
Learn more

Avoid relative imports for files in lib/.

build
Learn more

Annotate overridden members.

build thumb_up flutter
Learn more

Annotate redeclared members.

science build
Learn more

Return with no await.

build
Learn more

Avoid annotating with dynamic when not required.

build
Learn more

Avoid using as.

error
Learn more

Avoid bool literals in conditional expressions.

build
Learn more

Avoid catches without on clauses.

Learn more

Don't explicitly catch Error or types that implement it.

Learn more

Avoid defining a class that contains only static members.

Learn more

Avoid double and int checks.

Learn more

Avoid method calls or property accesses on a dynamic target.

Learn more

Avoid empty statements in else clauses.

build circles thumb_up flutter
Learn more

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

Learn more

Avoid escaping inner quotes by converting surrounding quotes.

build
Learn more

Avoid field initializers in const classes.

Learn more

Avoid final for parameter declarations.

build
Learn more

Avoid using forEach with a function literal.

build thumb_up flutter
Learn more

Avoid using 'FutureOr' as the type of a result.

science
Learn more

Don't implement classes that override ==.

Learn more

Don't explicitly initialize variables to null.

build thumb_up flutter
Learn more

Avoid JavaScript rounded ints.

Learn more

Don't declare multiple variables on a single line.

build
Learn more

Don't check for null in custom == operators.

report build
Learn more

Avoid positional boolean parameters.

Learn more

Avoid print calls in production code.

build flutter
Learn more

Avoid private typedef functions.

report build
Learn more

Avoid redundant argument values.

build
Learn more

Avoid relative imports for files in lib/.

build circles thumb_up flutter
Learn more

Don't rename parameters of overridden methods.

build thumb_up flutter
Learn more

Avoid return types on setters.

build thumb_up flutter
Learn more

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

error
Learn more

Avoid returning null for Future.

error
Learn more

Avoid returning null for void.

build thumb_up flutter
Learn more

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

Learn more

Avoid setters without getters.

Learn more

Avoid shadowing type parameters.

circles thumb_up flutter
Learn more

Avoid single cascade in expression statements.

build thumb_up flutter
Learn more

Avoid slow asynchronous dart:io methods.

Learn more

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

Learn more

Avoid types as parameter names.

circles thumb_up flutter
Learn more

Avoid annotating types for function expression parameters.

build
Learn more

Avoid unnecessary containers.

build flutter
Learn more

Avoid overriding a final field to return different values if called multiple times.

error
Learn more

Avoid defining unused parameters in constructors.

build
Learn more

Avoid async functions that return void.

build
Learn more

Avoid using web-only libraries outside Flutter web plugin packages.

flutter
Learn more

Await only futures.

build circles thumb_up flutter
Learn more

Name extensions using UpperCamelCase.

circles thumb_up flutter
Learn more

Name types using UpperCamelCase.

circles thumb_up flutter
Learn more

Cancel instances of dart:async StreamSubscription.

Learn more

Cascade consecutive method invocations on the same reference.

build
Learn more

Don't cast a nullable value to a non nullable type.

build
Learn more

Close instances of dart:core Sink.

Learn more

Invocation of various collection methods with arguments of unrelated types.

circles thumb_up flutter
Learn more

Sort combinator names alphabetically.

build
Learn more

Only reference in-scope identifiers in doc comments.

build
Learn more

Missing conditional import.

Learn more

Prefer using lowerCamelCase for constant names.

build thumb_up flutter
Learn more

Avoid control flow in finally blocks.

thumb_up flutter
Learn more

DO use curly braces for all flow control structures.

build circles thumb_up flutter
Learn more

Attach library doc comments to library directives.

build circles thumb_up flutter
Learn more

Depend on referenced packages.

circles thumb_up flutter
Learn more

Missing deprecated annotation.

Learn more

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

build
Learn more

DO reference all public properties in debug methods.

build
Learn more

Adhere to Effective Dart Guide directives sorting conventions.

build
Learn more

There should be no Future-returning calls in synchronous functions unless they are assigned or returned.

build
Learn more

Do not use environment declared variables.

Learn more

Document ignore comments.

Learn more

Avoid empty catch blocks.

build circles thumb_up flutter
Learn more

Use ; instead of {} for empty constructor bodies.

build thumb_up flutter
Learn more

Use ; instead of {} for empty container bodies.

build
Learn more

Avoid empty statements.

build thumb_up flutter
Learn more

Do use sound null safety.

error
Learn more

Put a single newline at end of file.

build
Learn more

Define case clauses for all constants in enum-like types.

build thumb_up flutter
Learn more

Name source files using lowercase_with_underscores.

circles thumb_up flutter
Learn more

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

build
Learn more

For synchronous values, Future.syncValue is more performant.

build
Learn more

Always override hashCode if overriding ==.

build circles thumb_up flutter
Learn more

Don't import implementation files from another package.

thumb_up flutter
Learn more

Explicitly tear-off call methods when using an object as a Function.

build circles thumb_up flutter
Learn more

Don't implicitly reopen classes.

science build
Learn more

Initialize the field in the field's initializer.

build
Learn more

Use case expressions that are valid in Dart 3.0.

science build
Learn more

Avoid runtime type tests with JS interop types where the result may not be platform-consistent.

circles thumb_up flutter
Learn more

Conditions should not unconditionally evaluate to true or to false.

error
Learn more

Invocation of Iterable<E>.contains with references of unrelated types.

error
Learn more

Join return statement with assignment when possible.

Learn more

Start multiline strings with a newline.

build
Learn more

Attach library annotations to library directives.

build circles thumb_up flutter
Learn more

Name libraries using lowercase_with_underscores.

Learn more

Use lowercase_with_underscores when specifying a library prefix.

thumb_up flutter
Learn more

Avoid using private types in public APIs.

thumb_up flutter
Learn more

Avoid lines longer than 80 characters.

Learn more

Invocation of remove with references of unrelated types.

error
Learn more

Boolean expression composed only with literals.

Learn more

Use matching super parameter names.

Learn more

Design widgets should be imported from Material or Cupertino packages.

build
Learn more

A code block is missing a specified language.

Learn more

Missing whitespace between adjacent strings.

Learn more

Don't use adjacent strings in list.

Learn more

No default cases.

science
Learn more

Don't use more than one case with same value.

build circles thumb_up flutter
Learn more

Avoid implicit casts from dynamic.

Learn more

Avoid leading underscores for library prefixes.

build thumb_up flutter
Learn more

Avoid leading underscores for local identifiers.

build thumb_up flutter
Learn more

Don't compare boolean expressions to boolean literals.

build
Learn more

Don't put any logic in createState.

flutter
Learn more

Avoid raw types.

Learn more

Avoid calling toString() on runtimeType.

Learn more

Don't assign a variable to itself.

Learn more

Don't use wildcard parameters or variables.

circles thumb_up flutter
Learn more

Name non-constant identifiers using lowerCamelCase.

build circles thumb_up flutter
Learn more

Noop primitive operations.

build
Learn more

Don't use null check on a potentially nullable type parameter.

build circles thumb_up flutter
Learn more

Do not pass null as an argument where a closure is expected.

build thumb_up flutter
Learn more

Omit type annotations for local variables.

build
Learn more

Omit obvious type annotations for local variables.

build
Learn more

Omit obvious type annotations for top-level and static variables.

build
Learn more

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

report
Learn more

Only throw instances of classes extending either Exception or Error.

Learn more

Don't override fields.

thumb_up flutter
Learn more

Provide doc comments for all public APIs.

error
Learn more

Use lowercase_with_underscores for package names.

thumb_up flutter
Learn more

Prefix library names with the package name and a dot-separated path.

Learn more

Don't reassign references to parameters of functions or methods.

Learn more

Use adjacent strings to concatenate string literals.

build thumb_up flutter
Learn more

Prefer putting asserts in initializer lists.

Learn more

Prefer asserts with message.

Learn more

Prefer using a boolean as the assert condition.

error
Learn more

Use collection literals when possible.

build thumb_up flutter
Learn more

Prefer using ??= over testing for null.

build thumb_up flutter
Learn more

Prefer const with constant constructors.

build
Learn more

Prefer declaring const constructors on @immutable classes.

build flutter
Learn more

Prefer const over final for declarations.

build
Learn more

Prefer const literals as parameters of constructors on @immutable classes.

build
Learn more

Prefer defining constructors instead of static methods to create instances.

Learn more

Use contains for List and String instances.

build thumb_up flutter
Learn more

Prefer double quotes where they won't require escape sequences.

build
Learn more

Use = to separate a named parameter from its default value.

error
Learn more

Use => for short members whose body is a single return statement.

build
Learn more

Private field could be final.

build thumb_up flutter
Learn more

Prefer final in for-each loop variable if reference is not reassigned.

build
Learn more

Prefer final for variable declarations if they are not reassigned.

build
Learn more

Prefer final for parameter declarations if they are not reassigned.

report build
Learn more

Prefer for elements when building maps from iterables.

build thumb_up flutter
Learn more

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

build
Learn more

Use a function declaration to bind a function to a name.

build thumb_up flutter
Learn more

Prefer generic function type aliases.

build circles thumb_up flutter
Learn more

Prefer if elements to conditional expressions where possible.

build
Learn more

Prefer using ?? operators.

build thumb_up flutter
Learn more

Use initializing formals when possible.

build thumb_up flutter
Learn more

Inline list item declarations where possible.

build thumb_up flutter
Learn more

Prefer int literals over double literals.

build
Learn more

Use interpolation to compose strings and values.

build thumb_up flutter
Learn more

Use isEmpty for Iterables and Maps.

build circles thumb_up flutter
Learn more

Use isNotEmpty for Iterables and Maps.

build circles thumb_up flutter
Learn more

Prefer is! operator.

build thumb_up flutter
Learn more

Prefer to use whereType on iterable.

build circles thumb_up flutter
Learn more

Prefer using mixins.

Learn more

Prefer null-aware method calls.

Learn more

Prefer using null-aware operators.

build thumb_up flutter
Learn more

Prefer relative imports for files in lib/.

build
Learn more

Only use double quotes for strings containing single quotes.

build
Learn more

Use spread collections when possible.

build thumb_up flutter
Learn more

Prefer typing uninitialized variables and fields.

build circles thumb_up flutter
Learn more

Don't use the Null type, unless you are positive that you don't want void.

build
Learn more

Provide a deprecation message, via @Deprecated("message").

circles thumb_up flutter
Learn more

Document all public members.

Learn more

Property getter recursively returns itself.

thumb_up flutter
Learn more

Deprecation in major version.

Learn more

Use trailing commas for all parameter lists and argument lists.

build
Learn more

Use secure urls in pubspec.yaml.

circles thumb_up flutter
Learn more

Use simple directive paths.

build thumb_up flutter
Learn more

Avoid unnecessary member names in variable patterns.

build thumb_up flutter
Learn more

SizedBox for whitespace.

build flutter
Learn more

Use SizedBox shrink and expand named constructors.

Learn more

Prefer using /// for doc comments.

build thumb_up flutter
Learn more

Sort child properties last in widget instance creations.

build flutter
Learn more

Sort constructor declarations before other members.

build
Learn more

Sort pub dependencies alphabetically.

build
Learn more

Sort unnamed constructor declarations first.

build
Learn more

Specify non-obvious type annotations for local variables.

build
Learn more

Specify non-obvious type annotations for top-level and static variables.

build
Learn more

Specify type annotations.

build circles thumb_up flutter
Learn more

Place the super call last in a constructor initialization list.

error
Learn more

Avoid switch statements on a 'Type'.

circles thumb_up flutter
Learn more

Test type of argument in operator ==(Object other).

Learn more

Avoid throw in finally block.

Learn more

Tighten type of initializing formal.

Learn more

Type annotate public APIs.

build
Learn more

Don't type annotate initializing formals.

build thumb_up flutter
Learn more

Don't use constant patterns with type literals.

build circles thumb_up flutter
Learn more

Future results in async function bodies must be awaited or marked unawaited using dart:async.

build
Learn more

Use of angle brackets in a doc comment is treated as HTML by Markdown.

circles thumb_up flutter
Learn more

No await no async.

science build
Learn more

Unnecessary await keyword in return.

report build
Learn more

Avoid using braces in interpolation when not needed.

build thumb_up flutter
Learn more

Don't use explicit breaks when a break is implied.

build
Learn more

Avoid const keyword.

build thumb_up flutter
Learn more

Don't use an explicit const in a generative enum constructor.

build
Learn more

Unnecessary .new constructor name.

build thumb_up flutter
Learn more

Don't use final for local variables.

build
Learn more

Avoid wrapping fields in getters and setters just to be "safe".

build thumb_up flutter
Learn more

Don't ignore a diagnostic code that is not produced.

build
Learn more

Don't create a lambda when a tear-off will do.

build
Learn more

Don't specify the late modifier when it is not needed.

build thumb_up flutter
Learn more

Avoid library directives unless they have documentation comments or annotations.

build
Learn more

Don't have a library name in a library declaration.

build thumb_up flutter
Learn more

Unnecessary new keyword.

build thumb_up flutter
Learn more

Avoid null in null-aware assignment.

build thumb_up flutter
Learn more

Unnecessary null aware operator on extension on a nullable type.

build
Learn more

Unnecessary null checks.

build
Learn more

Avoid using null in ?? operators.

build thumb_up flutter
Learn more

Use a non-nullable type for a final variable initialized with a non-nullable value.

build thumb_up flutter
Learn more

Don't override a method to do a super method invocation with the same parameters.

build circles thumb_up flutter
Learn more

Unnecessary parentheses can be removed.

build
Learn more

Unnecessary primary constructor bodies can be removed.

build
Learn more

Unnecessary raw string.

build
Learn more

Avoid using unnecessary statements.

Learn more

Remove unnecessary backslashes in strings.

build thumb_up flutter
Learn more

Unnecessary string interpolation.

build thumb_up flutter
Learn more

Don't access members with this unless avoiding shadowing.

build thumb_up flutter
Learn more

Use 'this' directly instead of assigning it to a local variable.

science build
Learn more

Unnecessary toList() in spreads.

build thumb_up flutter
Learn more

Don't use an explicit type name in a constructor.

build
Learn more

Unnecessary use of 'unawaited'.

build thumb_up flutter
Learn more

Unnecessary underscores can be removed.

build thumb_up flutter
Learn more

Unreachable top-level members in executable libraries.

build
Learn more

Equality operator == invocation with references of unrelated types.

build circles thumb_up flutter
Learn more

Avoid unsafe HTML APIs.

error
Learn more

Unsafe type: Has a type variable in a non-covariant position.

science
Learn more

Do not use BuildContext across asynchronous gaps.

flutter
Learn more

Use ColoredBox.

build
Learn more

Use a declaring parameter.

build
Learn more

Use DecoratedBox.

build
Learn more

Use enums rather than classes that behave like enums.

build
Learn more

Prefer an 8-digit hexadecimal integer (for example, 0xFFFFFFFF) to instantiate a Color.

build flutter
Learn more

Use generic function type syntax for parameters.

build thumb_up flutter
Learn more

Use ?? operators to convert nulls to bools.

report build
Learn more

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

Learn more

Use key in widget constructors.

build flutter
Learn more

Use late for private members with a non-nullable type.

science
Learn more

Use predefined named constants.

build
Learn more

If-elements testing for null can be replaced with null-aware elements.

build thumb_up flutter
Learn more

Use raw string to avoid escapes.

build
Learn more

Use rethrow to rethrow a caught exception.

build thumb_up flutter
Learn more

Use a setter for operations that conceptually change a property.

Learn more

Use string buffers to compose strings.

Learn more

Use string in part of directives.

build circles thumb_up flutter
Learn more

Use super-initializer parameters where possible.

science build thumb_up flutter
Learn more

Use throwsA matcher instead of fail().

Learn more

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

Learn more

Use truncating division.

build
Learn more

Use valid regular expression syntax.

circles thumb_up flutter
Learn more

Avoid declaring parameters with var and no type annotation.

science build
Learn more

Don't assign to void.

circles thumb_up flutter
Learn more