Linting your Dart Code!

Add strong lint rules in your Dart/Flutter project

Abhishek Upmanyu
3 min readDec 4, 2020

Hello fellow Flutterers!

Revolutionary ideas come to you when you’re supposed to be studying!

And so, it’s finally happening, in the middle of my end semester exam schedule, I’m finally here on Medium, writing my first article.

I’m Abhishek Upmanyu (heard that name, right? no, not that guy), an app dev with 2+ years of experience with Flutter, writing Flutter code since good old beta days.

Linting

Linting is the process of running a program that will analyse code for potential errors.

If you have even a tiny bit of experience with coding in some fancy IDE, you’d know exactly what linting is. It’s basically what gives you the errors, warning and weak warning, when you have some error in your code, or you violate style guidelines, or basically when your code makes your IDE angry.

Code depicting lint warning
Linter is responsible for that red warning thingy on mama() function (because the function isn’t returning a String).

Linter is what does linting. It reads and analyses the code for errors and warnings.

Setting Up Lint Rules

With dart, you can set up your own lint rules matching the code style you prefer.

To set up linting for your Dart/Flutter project, create analysis_options.yaml. This file should be at same level as your pubspec.yaml file.

analysis_options.yaml should be at root of your project

Adding Rules

Once created, we can add lint rules in our analysis_options.yaml file. These rules determine when to show an error, warning or weak warning.

To add lint rules, add linter as the top level key, followed by rules as the second level key. Under rules, you can specify what rules to enable by specifying the rule after a dash.

Example for adding lint rules

A list of all lint rules can be found here.

Using Lint Package

lint is a nice package for when you’re looking for strong lint rules for your project while maintaining control over what rules to enable. It is a collection of lint rules for dart.

lint is a hand-picked, open-source, community-driven collection of lint rules for Dart and Flutter projects. The set of rules follows the Effective Dart: Style Guide.

Installing

Start a new flutter project, or open up an existing project where you’d want to enable lint rules.

In pubspec.yaml, add lint as a dev_dependency

dev_dependencies:
lint:

After this, just ctrl+s your code and run flutter pub get.

Usage

For using lint, in your analysis_options.yaml, that we added in root directory of the project, add the following line at the start:

include: package:lint/analysis_options.yaml

That’s enough to get you sorted!

BUT, on top of these rules provided by lint package, you can also enable or disable linter and analyzer rules as per your convenience.

Example for setting lint rules

Below is my preference for lint rules, inspired from Reso Coder’s tutorials:

Conclusion

Enabling linting sets you up for writing pretty neat code; if you’re a beginner, it helps you learn code style, if you’re a pro, it saves time. With this, we’ve enabled linting in our project!

Further Reading 📖

Dart Guide for Analysis Options
Learn more about Effective Dart: Style.

Social 🤝🏼

Github: abhishekUpmanyu
LinkedIn: Abhishek Upmanyu
Instagram: Stuff by Abhishek Upmanyu

Thank You 🙌🏼

I hope this article comes as informative to anyone reading it. It’s been quite an experience writing my first one!

--

--

Abhishek Upmanyu

A peanut butter and flutter sandwich, writing flutter code since ‘18