Linting & Code Style
Linting in dart
The Dart SDK comes with a built-in linter in the analyzer package. To use it we need to create a configuration file that
holds the rules we want to use in our project.
Create the configuration file
In your root directy where your pubspec.yaml lives create a file named analysis_options.yaml
. In here you can make use
of every rule that can be found in the supported lint rules collection.
Here’s an example for the configuration
// Include the 'pedantic' package, that is maintained by Google and provides just a small set of rules
include: package:pedantic/analysis_options.1.8.0.yaml
analyzer:
exclude: [build/**]
strong-mode:
implicit-casts: false
linter:
rules:
- camel_case_types
Effective Dart
Effective Dart is a comprehensible guide of learnings, rules and
recommendations for coding with Dart. We recommend at least reading through the DOs & DONTs & other points mentioned
there in a short way.
Google itself just uses 27 of 150+ rules in their config. To enforce a stricter style we agreed on effective dart as our
base rules set. To make it easier to follow this community-driven style & coding guide we use the lint
package
found here.
Given our analysis_options.yaml
file we just need to import the package
include: package:lint/analysis_options.yaml
and we’re set. It’s never been this easy to enforce a good & strict code style. :)
To overwrite any rules just set them in the linter: rules: ...
section.