How to Improve Your Code Style with Lint Rules in Flutter?
After the end of the article you’ll be able to answer what is linting, how to implement it in flutter, how it will help to make dart code effective. In flutter there are already pre-build packages to implement linting which check the programmatic and code style errors such as, linter, lint, analyzer. These packages in flutter help in keeping the code healthy and up to the dart standards.
Table of Content
How Linting Helps?
How to write new linting rules?
Enable/Disable linting rules
What happens if we add all the rules to the existing repo?
Linter For Dart
This is an auto generated from our sources. Rules are organized into familiar rule groups. In addition, rules can be….
Linter | Dart Package
The Dart Linter package defines lint rules that identify and report on "lints" found in Dart code. Linting is performed in…
Analyzer | Dart Package
This package provides a library that performs static analysis of Dart code. It is useful for tool integration and…
How Linting Helps?
The main motive of linting is to get immediate intimations about identifying the hidden error which can improve your code quality or to recognize bugs that may cause hurdles while running the app. For instance, if you use Text (“demo”) in any of the widget then the analyzer will show a warning Prefer const with constant constructors, it means you have to keep this widget as const Text (“demo”), which is a quite good recommendation, and many times we forgot to split-it up using const keywords. So, we got know that linting is a really good practice.
How To Write New Linting Rules?
Let's move ahead to the most exciting part of linting i.e., to create lenting rules. We can change our settings for alerts when we use the wrong dart code format , we’ll get an error message. For this we’ve to edit analysis_options.yaml the file.
1. At project level create analysis_options.yaml file.
2. To get the error message, for wrong dart code we’ve to update the analyzer settings, in settings we’ll change the error, warning, ignore, info preferences of individual rules, for instance, missing_required_param: error, after that it will show error message every time you failed to meet the set requirements.
3. In addition to this we’ll add the linter which will control all missing arguments and will improve the code quality overall. For instance, avoid_empty_else , this will show an error message every time you do an empty statement.
Enable/Disable Linting Rules
Enabling/Disabling linting rules are super easy:
To check the code we’ve to run a dart analyzer. in the terminal.
What Happens If We Add All The Rules To The Existing Repo?
We’ve got almost 58 issues, which can decrease the app performance or can cause errors. So it's very important to be prepared in advance about the possible errors and how to fix them.
If you notice, these issues were not visible earlier, without adding linting and analyzer and after that solving all the issues the code quality and app performance got improved.
This file includes all analyzer and linter. We’ve tried to add all linter which can help you out. Under the analyzer errors tag, we can change the preferences of the linters, either you wanna show error or error warning for that selected linter
Applaud for you!! Finally you reached the end. We really hope we were able to give you the information about lint rules of flutter. You can also modify the code as per your needs. We hope you’ve got enough information about lint rules to create your own.