Flutter & Dart Lint

Recently, I wanted to add Dart Lint for our flutter projects. The core function of the Dart SDK analyzes the dart language is running an Analysis Server at a background process. Dart can get the whole syntax tree of the project and then execute syntax detection and analysis.

Adding Dart Lint for your project, you only need to create a file analysis_options.yaml under the root directory and set up some configuration.

Dart Lint Rules

The whole rules have many entries. Usually, we don’t need all of them. I list some configurations used by some famous companies.

  1. Effective Dart
  2. Google Team
  3. Flutter Official

Using Package to import lint rules

Dart Lints

The lint rules will change when the Flutter and Dart version upgrade. If you don’t want to update these rules manually, you can depend on a flutter lint plugin, and then you only need to include the plugin’s lint file in your analysis_options.yaml. Of course, you can still write rules in the file to override the imported rules.

  1. Install lints
1
dart pub add --dev lints
  1. Include lint file into analysis_options.yaml
1
include: package:lints/recommended.yaml

or

1
include: package:lints/core.yaml

Flutter Lints

The flutter lint contains some rules special for flutter.
Officially recommend version flutter_lints. The official flutter lint needs the Flutter version to be greater or equal to 2.12. If you are now using an old flutter version, you can use this community version.

  • Community flutter lint lint, its document is very clear.