What is Flutter Lint? How to resolve lint error effectively?
What is Flutter Lint? How to resolve lint error effectively?
It is important to learn code daily and have ways that really improve your code structure, readability, and maintainability. Its always a good idea to set some standards, some rules that will help make your code consistent. Writing code is not difficult what's difficult is writing high-quality maintainable code.

Every language has its own Linting rules and preferences based on the syntax of the language. Flutter also has some presets but they can be modified as per your requirements.

What is Linting?

The process of checking the source code for Styling errors and code formatting. It helps in identifying some mistakes in the code such as unused pieces of code, dead code, etc. I will be explaining the rules later in the article.

The tool used to analyze code for Linting is known as Lint or Linter.

Flutter provides a flutter_lints package preinstalled and ready to use when you create a Flutter project. It can be verified from pubspec.yaml file, at the root of the project, under dev_dependencies

Flutter Lints package in pubspec.yaml
Flutter Lints package in pubspec.yaml

Types of Linting

There are three types of Linting in Flutter:
  1. Errors: Common coding errors.
  2. Style: These rules focus on styles.
  3. Pub: These lints deal with pub related rules.

How to set Lint Rules for a Flutter Project:

Step 1: Create a Flutter Project

First of all create a Flutter project where we can test our newly set rules. Either open VS codes Command Palette by shortcut Ctrl+Shift+P and select `create project` oprion or use terminal command `flutter create project_name`.

Step 2: Add some of the linting rules:

Open analysis_options.yaml file and add some of the linting rules. For example, I'm adding the following:
Demo rules
Demo rules

You can add many other rules that can be found here.

Step 3: Add code

To test these rules you can create any small project where you can add some code you'll see all linting warnings under the problems section if you are using Visual Studio Code.

How to resolve them:

You can resolve most of the lint errors by modifying your Visual Studio Code settings file.

Goto settings.json file. To do so, open Command Palette by shortcut Ctrl+Shift+P and selecting Open User Setting (JSON).

Update the dart section as the following in the settings.json file:

settings.json my settings for dart
settings.json my settings for dart

You can read about effective dart styling here.

Do follow for more articles. Find me on any of the platforms mentioned on linktree.