Linter rules
Enforce consistent style, catch potential bugs early, and keep your Dart codebase maintainable with automated lint rules and quick fixes.
Overview
Each linter rule below documents what the rule checks, why it matters, BAD
and GOOD Dart code examples, and how to enable or configure the rule in
analysis_options.yaml.
Enabling rules
To enable custom lint rules in your project, configure the plugin diagnostics in your analysis_options.yaml
file:
plugins:
ilt_lints:
version: ^2.0.0
diagnostics:
export_library: true
prefer_const_constructors: true
prefer_primary_constructors: true
sort_fields: true
sort_functions_alphabetically: true
sort_functions_by_scope: true
sort_named_parameters: true
use_clock: true
use_file_system: true
valid_json_serializable: true
All linter rules
DO Export all library source files from your package's exporter library file.
Ensures all files matching any include glob (and not matching any optional
exclude glob) for a configured exporter
file are exported by that exporter. If a file overlaps include and
exclude, exclude wins.
DO Mark class constructors as const whenever the class can be constant without other changes.
Detects classes whose constructor (primary constructor or generative constructor) can be marked const
without any other changes and flags them if not already marked const.
DO Use Dart primary constructors for classes instead of declaring constructors in the class body.
Detects classes that do not use a Dart primary constructor (class Foo(...)
or class const Foo(...)) when a primary constructor can be used.
DO Sort fields above methods by scope, finality, and alphabetically within each group.
Ensures fields within a type body (classes, mixins, enums, extensions, and extension types) appear before all methods, getters, setters, and operators (ignoring constructors), grouped by scope (public static const, private static const, public static, private static, public instance, private instance), with
final fields before non-final fields when
final_first is enabled, and sorted alphabetically within each group.
DO Sort functions, getters, and setters alphabetically within each scope.
Ensures functions, getters, and setters within a type body (classes, mixins, enums, extensions, and extension types) are sorted alphabetically (lexicographically) within each scope. Fields, constructors, and operators are ignored.
DO Sort functions, getters, and setters by static/instance and visibility scope.
Ensures functions, getters, and setters within a type body (classes, mixins, enums, extensions, and extension types) are sorted by scope: public static getters, public static setters, public static functions, private static getters, private static setters, private static functions, public getters, public setters, private getters, private setters, public instance functions, and private instance functions. Fields, constructors, and operators are ignored.
DO Sort named parameters in declarations and named arguments at call sites alphabetically.
Ensures named parameters in function, method, and constructor declarations and named arguments at call sites are sorted alphabetically. Leading underscores on private named parameters are ignored when sorting so
_foo sorts as foo. Supports required_first
and ignore options in analysis_options.yaml.
DO Use clock.now() from package:clock/clock.dart
instead of DateTime.now() in lib/ files.
Detects usages of DateTime.now() and DateTime.now
tear-offs in files inside the lib/ folder (ignoring test/
and other non-lib/ directories) and requires using clock.now()
/ clock.now from package:clock/clock.dart
so that time can be mocked in tests.
DO Use FileSystem from package:file/file.dart
instead of dart:io File, Directory,
FileSystemEntity, and Link in lib/
files.
Detects direct usages of dart:io File,
Directory, FileSystemEntity, and Link
in files inside the lib/ folder (ignoring test/
and other non-lib/ directories) and requires using FileSystem
from package:file/file.dart so file system operations can be mocked in tests.
DO Include the generated .g.dart part directive and define fromJson
and toJson methods that call the generated json_serializable
helpers.
Ensures classes annotated with @JsonSerializable include the generated
part '<filename>.g.dart'; directive, define a valid fromJson
factory or static method calling _$<ClassName>FromJson when createFactory
is enabled, and define a valid toJson method calling _$<ClassName>ToJson
when createToJson is enabled.