How to add Lint rules in Flutter

⋅ 2 min read ⋅ 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).

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

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:

  1. Open the analysis_options.yaml file in the project root folder. This file is created for you by the flutter 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
  1. Add your rules in analysis_options.yaml under the rules key. Here is an example where I add an extra prefer_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.

Sponsor sarunw.com and reach thousands of iOS developers.

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:

  1. Add a dev_dependency on package:flutter_lints to your project's pubspec.yaml by running the following command in the project's root directory.
flutter pub add --dev flutter_lints
  1. 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 Share
Previous
How to set UserDefaults value with Launch Arguments

Learn how to use a launch argument to override UserDefaults value and test your apps.

Next
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.

← Home