Static Analysis & Linters in Dart

Tools that helps you find the bugs before you execute

What is Static Analysis

As we know, Software testing is performed to measure the quality of products in the development stage. One of the testing techniques is Static Test. This can be divided into two major categories, Static Analysis and Reviews.

Static Test Techniques - from softwaretestinghelp.com

In Reviews, developers get feedback and discuss the issues with individuals or teams. This category can be divided into furthermore sub-sections, Informal, Walkthrough, Peer Review and Inspection.

Static Analysis is done by automated tools which can be performed without executing the codes. The main concept of this is, Analyze the code written by developers with the help of tools. The main reason of use static analysis is to check the code structure and industry standards.

Static Analysis in Dart

To perform static analysis in Dart, you need the help of the analyzer package. By default, you get it with dart-sdk. You can customize your dart static analysis for errors, warnings, and other potential issues. For this, we need to add analysis_options.yaml to our project. Here, we can add/update the different rules for analysis. In addition to this, you can add any lint rule from the effective dart.

Set up analysis_options.yaml

If you create a new flutter project, analysis_options.yaml will come by default with the project. But if your project is old and the analysis_options.yaml is missing, you can add it at the root level of your project, in the same directory the pubspec.yaml contains. The file name will be analysis_options.yaml.

After adding the file, you can include some predefined lint rules from a package. For example, flutter_lints package provides a lot of linter rules. We can add this package in our dev_dependencies. Then we can include it inside our analysis_options.yaml file.

dev_dependencies:
  flutter_lints: ^1.0.0
include: package:flutter_lints/flutter.yaml

In the second section, we will add an analyzer section. Here, we can do: enabling stricter type checks, excluding files, ignoring specific rules, changing the severity of rules, or enabling experiments.

In the last section, the linter will be placed. New rules will be inserted in this section. For example, we want to avoid print in our code. We can add this rule to this section and it will start working.

Demo

I have added analysis_options.yaml in my project and also added the flutter_lints package. Here is the code of my analysis_options.yaml.

include: package:flutter_lints/flutter.yaml

analyzer:
  errors:
    prefer_const_declarations: warning
    avoid_print: error

linter:
  rules:
    prefer_const_declarations: true
    avoid_print: true

In the code above, I have included the flutter_lints package rules and also added some custom linters. prefer_const_declarations is the lint to add const wherever a constant declaration is possible, especially on the widgets tree to stop unnecessary rebuilds. For this lint rule, we have customized the analyzer level into warning. In the next rule, avoid_print is used for avoiding the print to be used in code and if you do, you will get an error for that. Since we have changed the analyzer section for this lint to error level by avoid_print: error .

Linter Packages