This package contains a recommended set of lints for Flutter apps, packages, and plugins to encourage good coding practices.
It is essentially a combination of three sources:
- Dart's package:lints — its
recommendedset (which itself includes thecoreset). This package depends onlintsdirectly, so it picks up every compatible release oflints; a new major version oflintsneeds a new release of this package. - Flutter's package:flutter_lints — its Flutter-specific rules (for example
use_key_in_widget_constructorsanduse_build_context_synchronously). These are enabled directly rather than by depending onflutter_lints, becauseflutter_lintstends to lag behindlints, and depending on it has at times heldlintsback to an older version. - A curated selection of additional lints from the full list of Dart linter rules that further encourage good coding practices.
Lints are surfaced by the Dart analyzer, which statically checks Dart code. Dart-enabled IDEs typically present the issues identified by the analyzer in their UI. Alternatively, the analyzer can be invoked manually by running flutter analyze.
Analyzer settings
Besides lint rules, the included file also configures the analyzer:
- It turns on the
strict-inferencetype check, which reports code where the analyzer can't infer a type and would otherwise fall back todynamic. - It reports implicit casts from
dynamic(no_dynamic_casts) as errors and generic types without type arguments, such asList(no_raw_types), as warnings. These two lints replace the olderstrict-castsandstrict-raw-typestype checks. - It raises
missing_required_param,missing_return, andrecord_literal_one_positional_no_trailing_commato errors, andcollection_methods_unrelated_typeandunrelated_type_equality_checksto warnings. - It excludes
generated_plugin_registrant.dartand.test_coverage.dartfiles from analysis.
Any of these can be changed in your own analysis_options.yaml. For example, no_dynamic_casts: false under linter: rules: turns off the check for implicit casts from dynamic, and no_raw_types: info under analyzer: errors: lowers that check to an info.
Reading flutter.yaml
lib/flutter.yaml lists every Dart lint rule in alphabetical order, so it shows what this package leaves off as well as what it turns on:
- rule_nameis enabled by this package. A trailing# flutter-lintsmeans the rule also comes frompackage:flutter_lints.# - rule_name # dart-lang core(or# dart-lang recommended) is already enabled bypackage:lints, so it's listed only for completeness.# DISABLE - rule_nameis deliberately left off.# NEW, DISABLE(or# NEW, DISABLE FOR NOW) marks a rule that was left off when it was added to Dart.# EXPERIMENTAL - rule_nameis experimental in Dart and left off. A few experimental rules that work well, such asunnecessary_async, are enabled.# DEPRECATED - rule_nameand# REMOVED - rule_namehave been deprecated or removed from Dart.# NEW!marks a rule enabled in the latest release of this package; the changelog lists them.
To turn on a rule that this package leaves off, add rule_name: true under linter: rules: in your own analysis_options.yaml.
Usage
Enable these lints by following these instructions:
- Depend on this package as a dev_dependency by running
flutter pub add --dev flutter_lints_plus. - Create an
analysis_options.yamlfile at the root of the package (alongside thepubspec.yamlfile) andinclude: package:flutter_lints_plus/flutter.yamlfrom it.
Example analysis_options.yaml file:
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.
# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints_plus/flutter.yaml
linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints_plus/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart.dev/tools/linter-rules/all.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# public_member_api_docs: true # Uncomment to enable the `public_member_api_docs` rule
# Additional information about this file can be found at
# https://dart.dev/tools/analysis