10 Important Lint Rules For Clean Flutter Projects

10 Important Lint Rules For Clean Flutter Projects

Lint rules have several benefits, like providing standardization and better code quality, predicting run time errors...

As you know, flutter_lints package has started to support our new Flutter projects for a while. On the other hand, we are not limited by these rules. There are more than rules in SDK. We can also get rules from lint over more built-in ones. Just a second... is it over? No, you can create your own rules too. However, it is not the topic of this article.

I'd like to underline that we can't say that each rule is compatible with our projects. It depends on our project development paths. In addition, some rules can conflict with each other. So, we should scrutinize selecting lint rules for projects.

Today, I'm sharing 10 important lint rules for clean code and architecture which not served by default. I've experienced these rules on large scaled Flutter projects and I'm so satisfied to use them. Let's begin:

1. lines_longer_than_80_chars
https://dart-lang.github.io/linter/lints/lines_longer_than_80_chars

2. require_trailing_commas (best rule ever)
https://dart-lang.github.io/linter/lints/require_trailing_commas
https://dcm.dev/docs/individuals/rules/common/prefer-trailing-comma

3. avoid-banned-imports (excellent rule for layered architecture)
https://dartcodemetrics.dev/docs/rules/common/avoid-banned-imports

4. avoid-global-state
https://dartcodemetrics.dev/docs/rules/common/avoid-global-state

5. no-magic-number
https://dartcodemetrics.dev/docs/rules/common/no-magic-number

6. prefer-extracting-callbacks
https://dartcodemetrics.dev/docs/rules/flutter/prefer-extracting-callbacks

7. prefer-single-widget-per-file
https://dartcodemetrics.dev/docs/rules/flutter/prefer-single-widget-per-file

8. avoid-nested-conditional-expressions
https://dartcodemetrics.dev/docs/rules/common/avoid-nested-conditional-expressions

9. prefer-match-file-name
https://dartcodemetrics.dev/docs/rules/common/prefer-match-file-name

10. always_declare_return_types
https://dart-lang.github.io/linter/lints/always_declare_return_types.html

After these rules above, I also suggest taking a look at linters related to null safety, member ordering, type defining and asynchronous operations.

Thanks for reading :)
Levent Kantaroglu