Linting in Flutter
Flutter is a powerful mobile app development framework that allows you to build high-quality apps. But with great power comes great responsibility, and one of your responsibilities as a developer is to write clean and maintainable code
That’s where linting comes in.
History lesson 💤and some general knowledge you can skip this part
Linting is my new word of the day.
Linting originated from a Unix utility that is used to examine c language source code. Lint is a static code analysis tool used to flag programming errors, bugs, and stylistic errors basically helping programmers write better code and fix some errors while they code. Linting is used in almost all famous languages and today we are going to learn about linting in flutter.
So, Linting helps us code better by flagging errors and stylistic errors but why bother? I mean why should I bother to put extra effort to clear out stylistic errors or bugs in my flutter code?
Let's take an example to explain this. We all might have come across a famous lint that tells you to add a const keyword before a function or a class when you are developing a flutter app. Often times it might get frustrating 🤦🏽♂️to add the “const” keyword before a widget but in reality, it actually helps the compiler better decide which widget to better allocate more resources, in short, it helps improve code and performance.
(To get a detailed breakdown of the const keyword in the dart, refer to this medium article: https://medium.com/flutter-community/the-flutter-const-keyword-demystified-c8d2a2609a80)
UsingUsing) lint not only helps with performance enhancement but some lint help you detect errors as you code thereby saving a lot of time. Another important aspect about using lint is it generally makes your code readable unless, of course, you write really horrible code.
How to set up lint in Flutter?
Rules are organized into familiar rule groups.
errors — Possible coding errors.
style — Matters of style, largely derived from the official Dart Style Guide.
pub — Pub-related rules.
(If you want to learn more about rules in dart you can check out this website made by Google’s Dart and Flutter team. https://dart-lang.github.io/linter/lints/)
Unlike before, Nowadays when you run the command
flutter create myapp
it automatically generates analysis_options.yaml
If you are using an older version of flutter you might want to manually add the file analysis_options.yaml
to the root of your flutter project
Step 1:
We will be enabling lint rules with flutter_lints package
you might already have this package in your pubspec.yaml
file but if you don't add this package inside your dev_dependencies
.
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
Then run the command flutter pub get
to update your dependencies.
Once this is done your project will start using the existing lint rules that are given by the dart team.
Step 2
In the analysis_options.yaml
file you can set up custom lint and add or remove lint that is already defined by the dart and flutter team.
As you can see in the rules section of the code I have added two lint.
Avoid print
prefer single quotes(I will remove or comment this because I don't prefer single quotes)
# 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: true # 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
This website => Lints contains rules provided by google’s Flutter and dart team. An example of a rule in this will be
throw_in_finally
lint that helps us avoid throw statements inside finally block.
You can use these lint by adding them into the analysis_options.yaml
file.
Something like 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: true # Uncomment to disable the `avoid_print` rule
prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
throw_in_finally: true
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
You can specify specific rules for specific projects, flutter enables you to make new lint rules that might help you build lint that is specific to your project.
Before assigning any lint to your projects don't forget to check out what and how the lint work so that you don't end up with some different results. You can do this by visiting the lint package website which will help you understand what specific lint rules are and how to use them.
fin