Improving Your Flutter Code

Posted on Jun 17, 2019

It doesn’t matter how many years we have been working as developers, we will always make the same mistakes: deploy code which is far from clean.

Here’s where testing and static analysis can help us. Static analysis checks our code without running it, looking for programming errors, and it is also known as linting or lint analysis. Tests instead, look for implementation errors by running our code.

If you are writing Flutter apps, you should be writing as well tests, that’s an essential part of software development, and since testing is way easier than with native application development, we have no excuse.

However, writing those tests is not useful if you are not running them periodically. And the same can be said about static analysis.

My problem is that always I forget. I code, commit, push… and then receive an email from my Continuous Integration service telling me a test failed or there’s a new lint error.

To avoid that, I run both commands before pushing code. And to do it automatically, I use a git hook. A git hook is a script that will run before or after a git command. In my case, I want to run a script before pushing code.

You can do it this way:

Create a file named pre-push inside the .git/hooks folder of your project Make the file executable with chmod +x Copy the following code in that file:

This shell script will run before pushing code to git, and will cancel the push command if something went wrong.

In detail, the script is first stashing any uncommited changes, runs both flutter analyze and flutter test and if any of those fails, it will pop the stashed changes back and cancel the push command.

If both run, the push completes and the code is published, then the uncommited changes are pop back in place. In short, any uncommited changes are going to be stashed and pop once the scripts ends.


After using flutter analyze for a while, I felt that something was missing.

For example, I wanted to get rid of the new keyword and I wanted to use const whenever was possible. There’s when I created my own analysis options.

To do so, create a file named analysis_options.yaml into your Flutter project.

For starters, you can copy the contents from https://github.com/flutter/flutter/blob/master/packages/flutter/lib/analysis_options_user.yaml which provide the same lint checks as the default Flutter projects.

And then you can add whatever you like, for example:

- prefer_const_constructors_in_immutables
- prefer_final_fields
- prefer_final_in_for_each
- prefer_final_locals
- unnecessary_new I am currently trying prefer_double_quotes but it also
  complains about imports… which are automatically added with single quotes 😅

You can find all of them here: https://dart-lang.github.io/linter/lints/

With automatic analysis and tests running on each git push, together with custom lint rules, you will quickly improve the quality of your codebase without noticing.

I hope you enjoyed this article, please feel free to reach out!

Share Tweet