A month of Flutter: continuous linting

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

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

$ 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)

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…


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.

Add the following to .travis.yml:

  - name: Test
    env: COMMAND=test
  - name: Analyze
    env: COMMAND=analyze

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

  - ./flutter/bin/flutter $COMMAND

The matrix option 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 consts where the analyzer says. Using const lets the Dart compiler better optimize your code and improve performance.

Come back tomorrow when I'll be talking about Flutter's big announcement.

Code changes

Posts in this series

  • A month of Flutter
  • A month of Flutter: create the app
  • A month of Flutter: configuring continuous integration
  • A month of Flutter: continuous linting
  • A month of Flutter: upgrading to 1.0
  • A month of Flutter: initial theme
  • A month of Flutter: no content widget
  • A month of Flutter: a list of posts
  • A month of Flutter: extract post item widget

  • Category: Development