Quantcast
Channel: Stories applauded for by Greg Perry on Medium
Viewing all articles
Browse latest Browse all 16

Setting up Lint Rules in Dart-Flutter

$
0
0

Day by day we learn new stuff. Day by day we aspire to improve our Code content and implementing them is usually a dream come true. There’s nothing as difficult as deciphering someone else’s code. It is usually very hard to understand especially when no particular format is followed unless the person wrote clear readable lines of Code. Well as the proverb goes, “Where there are rules, there’s law, “ It’s good to set some standard rules to follow when writing code so that someone else can understand or refer to especially in team projects. You might be wondering what lint/linter rules are, keep calm, I will take you through step by step procedures on how to set up Lint rules in Dart.

NB: Lint rules can be set in other languages too like Javascript, PHP, CSS, Python, Elixir among others.

If you're new to Flutter, follow these installation guides to get started in Flutter.

Install

What’s Linting?

Linting is the process of checking the source code for Programmatic as well as Stylistic errors and unformatted code. It’s helpful in identifying some common and uncommon mistakes that are made during coding like logical errors, unused variables, empty if-else statements among others.

Linter/Lint is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs. It supports verifying code quality.

Examples of some Common and Uncommon errors.

To get a much more visual view of what I mean, you may have noticed some lines get underlined in your code as shown below; for Unused variables or unused imports.

Example of unused imports

In the above image, the underlined import in green shows that the file has been imported but has not been used. It’s appropriate to always use the imported files or remove them if they ain't necessary.

Example of unused variables.

The above image shows an example of unused variable underlined in a green squiggle. Whenever you counter such it’s advisable to either remove the unused variable or use it in the code.

Example of unused fields

The above image shows an example of unused fields underlined in green. The value of _controller in the image above is not used. You can either remove it or try using it for effective dart code.

How do we set up lint rules in Dart?

It’s usually advisable to set up lint rules at the beginning of a project rather than after starting a project since the warnings to be fixed might pile up.

There is usually a set of default linting configuration after setting a project in an IDE, however, you can customize your own set of rules to your specification to have a well-formatted code.

Rules are categorized into these major groups:

  • Error rules -These are regarding common errors that occur when coding.
  • Style rules -These are rules focusing on style as stated by Dart Style Guide.
  • Pub- These are pub-related rules.

The above image shows a list of rules as stipulated by the Google team concerning style guides. You can find more about this in

Linter for Dart

Step 1

At the root of your project, create a file named analysis_options and place the file as analysis_options.yaml in the same directory as the pubspec file.

analysis_options file at the root of the project

There are two ways of enabling lint rules in Dart:

  1. Enabling default Google rules: pedantic

These are rules that come with already set lint rules and they depend on Pedantic package :

pedantic | Dart Package

Place the package in pubspec.yaml file as:

Add a normal dependency if you're using “package:pedantic/pedantic.dart” like in the above image.

If you only want analysis_options.yaml, place the package as dev-dependencies.

Run the command flutter packages get in your terminal to update the dependencies, then add the following line to your analysis_options.yaml file.

N/B: It's recommended that you update code to work with new rules whenever a new version of the pedantic package is released.

2. Enabling individual rules.

These are rules that an individual sets on their own according to their own specifications without using the pedantic package that comes with already set rules. When enabling individual linter rules, add linter: to the analysis-options file, followed by rules: Then below that line, specify the rules that you want to apply, prefixed with dashes. For instance:

Before setting individual rules from the Dart linter style guides, always read and understand the rule and check how it works, for example:

example of a linter rule

After setting up the linter rules, run the command flutter analyze to analyze and check your code.

You can read more on this from Effective Dart Style and customizing static analysis from:

Customizing static analysis

Happy fluttering, but remember to Dart before you Flutter, please find these resources helpful: How to get started on Flutter.

Thank you 😍


Setting up Lint Rules in Dart-Flutter was originally published in podiihq on Medium, where people are continuing the conversation by highlighting and responding to this story.


Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>