How to add Lint rules in Flutter
Table of Contents
As your team and project grow, you might need to enforce some coding rules to make your code go the same direction. Luckily, adding custom lint rules in Flutter is very easy (If you use a new Flutter version).
Flutter version 2.3.0-12.0.pre or newer
Projects created with flutter create
using Flutter version 2.3.0-12.0.pre or newer come with pre-installed package:flutter_lints. This package contains a recommended set of lints for Flutter apps to encourage good coding practices.
To add your custom rules:
- Open the
analysis_options.yaml
file in the project root folder. This file is created for you by theflutter create
command. You should see a boilerplate similar to this.
# 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/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/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# 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
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
- Add your rules in
analysis_options.yaml
under therules
key. Here is an example where I add an extraprefer_relative_imports
rule.
include: package:flutter_lints/flutter.yaml
linter:
rules:
- prefer_relative_imports
You can find all available rules here.
You can easily support sarunw.com by checking out this sponsor.
AI Grammar: Correct grammar, spell check, check punctuation, and parphrase.
Old Flutter version
If you are on a project that was created with an old Flutter version, you have to do a little setup.
You need to do two things:
- Add a dev_dependency on
package:flutter_lints
to your project'spubspec.yaml
by running the following command in the project's root directory.
flutter pub add --dev flutter_lints
- Create an
analysis_options.yaml
file in the root directory of your project with the following content:
include: package:flutter_lints/flutter.yaml
linter:
rules:
And now you can start filling in your own rules.
Read more article about Flutter or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareHow to set UserDefaults value with Launch Arguments
Learn how to use a launch argument to override UserDefaults value and test your apps.
How to modularize existing iOS projects using Swift Package
Modular programming is a software design technique that breaks your project into a smaller maintainable module which promotes separation of concern and reusability. Let's see how easy it is to modularize an iOS app with Swift Package.