How to use linter in flutter/Dart to improve code quality?

Asked

Viewed 165 times

0

In flutter/Dart Lint (process that parses the code to identify possible errors.) comes by default with some 'flexible' rules that allows an error code to compile and execute.

Such rules can pass up serious errors such as:

FloatingActionButton(
  child: Icon(Icons.announcement),
);

warning The parameter 'onPressed' is required

The missing parameter onPressed cause only one warning when you really shouldn’t even compile.

Another case that is quite common is the omission of const which may result in loss of performance, more details here.

Text(
    'You have pushed the button this many times:',
),

How to configure Lint to ensure higher code quality?

  • 1

    I don’t know if you’ve found anything specific to the cases you mentioned, but as far as I know there are some predefined rules that can be customized. Also I did not get to use but I will leave here some references that I found that might be useful to you: -https://medium.com/podiihq/setting-up-lint-rules-in-dart-flutter-1ebbed0418a6 (a quick Tuto) / https://dart-lang.github.io/linter/linlints/ (Dart lints) / https://dart.dev/guides/language/analysis-options (reference in doc of Dart)

  • 1

    @Leonardopaim Thank you, I’ll read the documentation

1 answer

0

There are some packages with stricter rules that guarantee better quality and standardization of the code, follow some of them:

Like any other package you need to add it to the project through pubspec.yaml, using with example the package lint the setting would be:

dev_dependencies:
  lint: 

dev_dependencies - Because it is a package using only on development environment

To use the new rules create the (analysis_options.yaml) in the project root folder and add the rules settings:

include: package:lint/analysis_options.yaml

To customize see the documentation on Customizing Static analysis

Browser other questions tagged

You are not signed in. Login or sign up in order to post.