Sunday, June 30, 2024
Google search engine
HomeImprove code style using lint rules in Flutter

Improve code style using lint rules in Flutter

Learn how we can make dart code absolutely BUG FREE

In this blog, we shall learn what is linting, how we can implement it in the flutter repository, how it helps to make our dart code effective. There are some packages that are available for us to implement linting into our flutter app that checks for programmatic and stylistic errors such as lint, analyzer, linter. All these packages help to keep the health of the code good and up to the dart standards.


Table Of Contents ::

Important links

Who linting helps?

Writing new linting rules

Enable and disable linting rules

What happens when I added all the rules to my existing repo

analysis_options.yaml

Conclusion



Important links:

Linter for Dart
This list is auto-generated from our sources. Rules are organized into familiar rule groups. In addition, rules can be…dart-lang.github.io

linter | Dart Package
The Dart Linter package defines lint rules that identify and report on “lints” found in Dart code. Linting is performed in…pub.dev

analyzer | Dart Package
This package provides a library that performs static analysis of Dart code. It is useful for tool integration and…pub.dev

How linting helps?:

The purpose of linting is to get real-time suggestions about identifying the hidden error that may improve the code quality or to identify bugs that may cause issues while running the app. For example, if you use Text(“demo”) in any widget then the analyzer will show a warning Prefer const with constant constructors , it means you should keep this widget as const Text("demo") , which I think is really a good suggestion, and most of the time we forget to initialize it using const keywords so I found that using linting is really good practice.

Writing new linting rules:

Now let us see how we can change our settings so that we get an error message every time we use the wrong dart code format. To do this we need to edit analysis_options.yaml the file.

  1. Create analysis_options.yaml a file at the project level.
  2. To show the error message we need to change the analyzer settings, in analyzer we will change the error, warning, ignore, info preferences of individual rules, for example missing_required_param: error , it will always show an error message every time you miss the required parameter.
  3. Also, we will add the linter that will handle all the errors causing missing arguments or might improve the code quality. eg. avoid_empty_else this linter will show an error message whenever we will declare an empty else statement.

Enable and disable linting rules:

We can easily enable and disable linting rules:

linter:

rules:
always_declare_return_types: false
annotate_overrides: true

To analyze the code we need to run dart analyzer . in the terminal.

What happens when I added all the rules to my existing repo?:

I got the following 58 issues, that might decrease my app performance or might cause any issue without an error message so it’s really important to know all the possible errors and solve them first.

All these issues were not visible before the linting and analyzer were added, after resolving all the issues the code quality is really improved and app performance is also improved.

AFTER RESOLVING ALL THE ERROR

analysis_options.yaml:

This file contains all the linter and analyzer. I have added all the linter that might help you. Under the analyzer errors-tag, we can change the preferences of the linters whether we want to show errors or warnings related to that particular linter.

https://gist.github.com/anmolgupta-aeologic/ccfdcaa5e214c070ab938feeb86e49b9#file-analysis_options-yaml

Conclusion:

In the article, I have explained the basic structure of Lint Rules in a flutter; you can modify this code according to your choice. This was a small introduction to the Lint Rules On User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying up the Lint Rules in your flutter projects. In this blog, we have examined the Lint Rules. I hope this blog will help you in the comprehension of the Lint Rules in a better way. So please try it.

🌸🌼🌸 Thank you for reading 🌸🌼🌸

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular