A month of Flutter: continuous linting

Abraham Williams - Dec 4 '18 - - Dev Community

Originally published on bendyworks.com.

Yesterday I configured CI. Today, in step with @tpolansk's advice, lets set up linting. For this project we'll start with the analysis options that the Flutter team uses. To download the file I'll run the following command within the root directory of the repository.

$ wget https://raw.githubusercontent.com/flutter/flutter/master/analysis_options.yaml
~~~{% endraw %}

The {% raw %}`analysis_options.yaml`{% endraw %} file tells the {% raw %}`dartanalyzer`{% endraw %} tool the preferences to use when analyzing the codebase. You can now run {% raw %}`flutter analyze`{% endraw %} to see everything in the app that can be improved. Your editor should use this file to show you inline feedback as well.{% raw %}

~~~bash
$ flutter analyze
Analyzing app...

   info • Prefer declare const constructors on `@immutable` classes • lib/main.dart:29:3 • prefer_const_constructors_in_immutables
   info • Prefer const with constant constructors • lib/main.dart:94:13 • prefer_const_constructors
   info • Prefer const with constant constructors • lib/main.dart:107:16 • prefer_const_constructors

3 issues found. (ran in 0.8s)
~~~{% endraw %}

Linting is one of the tooling options you want to get into a project as soon as possible so you don't run into issues like @hillelcoren did.

> A bit depressing seeing the number of warnings go from 2 to 2,000 but better now than later...
>
> [@hillelcoren](https://twitter.com/hillelcoren/status/1013337198295441409)

But having linting set up in your app doesn't mean developers will run or follow the recommended patterns. So let's enforce it with the [CI we set up yesterday](https://bendyworks.com/blog/a-month-of-flutter-configuring-continuous-integration).

Add the following to {% raw %}`.travis.yml`:

~~~yaml
matrix:
  include:
  - name: Test
    env: COMMAND=test
  - name: Analyze
    env: COMMAND=analyze
~~~

And modify the `script` line to use `$COMMAND` instead of `test` like this:

~~~yaml
script:
  - ./flutter/bin/flutter $COMMAND
~~~

The [`matrix` option](https://docs.travis-ci.com/user/customizing-the-build#build-matrix) on Travis CI is very powerful. It lets you set up multiple test runs that will perform in parallel.

Now we need to make the suggested improvements. In this case it's as simple as adding a couple of {% raw %}`const`{% endraw %}s where the analyzer says. Using {% raw %}`const`{% endraw %} lets the Dart compiler [better optimize your code](https://stackoverflow.com/a/47996512/26406) and improve performance.

Come back tomorrow when I'll be doing talking about Flutter's [big announcement](https://developers.googleblog.com/2018/12/flutter-10-googles-portable-ui-toolkit.html).

## Code changes

- [#7 Run flutter analyze on CI](https://github.com/abraham/birb/pull/7)
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .