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…

@hillelcoren

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:

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:

script:
- ./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
  • A month of Flutter: post model and mock data
  • A month of Flutter: rendering a ListView with StreamBuilder
  • A month of Flutter: Stream transforms and failing tests
  • A month of Flutter: real faker data
  • A month of Flutter: rendering network images
  • A month of Flutter: FABulous authentication
  • A month of Flutter: configure Firebase Auth for Sign in with Google on Android
  • A month of Flutter: configure Firebase Auth for Sign in with Google on iOS
  • A month of Flutter: Sign in with Google
  • A month of Flutter: mocking Firebase Auth in tests
  • A month of Flutter: delicious welcome snackbar
  • A month of Flutter: navigate to user registration
  • A month of Flutter: user registration form
  • A month of Flutter: testing forms
  • A month of Flutter: setting up Firebase Firestore
  • A month of Flutter: awesome adaptive icons
  • A month of Flutter: set up Firestore rules tests
  • A month of Flutter: Firestore create user rules and tests
  • A month of Flutter: WIP save users to Firestore
  • A month of Flutter: user registration refactor with reactive scoped model
  • A month of Flutter: the real hero animation
  • A month of Flutter: a look back

  • Category: Development