Aller au contenu

Linter Overview

Ce contenu n’est pas encore disponible dans votre langue.

Linting is the process of statically analyzing code for potential bugs in addition to programmatic and stylistic errors.

Bloc has a built-in linter, which can be used through your IDE or the bloc command-line tools with the bloc lint command.

With the help of the bloc linter, you can improve the quality of your codebase and enforce consistency without executing a single line of code.

For example, perhaps you accidentally imported a Flutter dependency into your cubit:

counter_cubit.dart
import 'package:bloc/bloc.dart';
import 'packages:flutter/material.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
}

If properly configured, the bloc linter will point to the import and produce the following warning:

Fenêtre de terminal
warning[avoid_flutter_imports]: Avoid importing Flutter within bloc instances.
--> counter_cubit.dart:2
|
| import 'package:flutter/material.dart';
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= hint: Blocs should be decoupled from Flutter.
docs: https://bloclibrary.dev/lint-rules/avoid_flutter_imports
1 issue found
Analyzed 1 file

In the following sections, we’ll cover how to install, configure, and customize the bloc linter so that you can take advantage of static analysis in your codebase.

Get started using the bloc linter in just a few quick and easy steps.

  1. Install the bloc command-line tools

    Fenêtre de terminal
    dart pub global activate bloc_tools
  2. Install the bloc_lint package

    Fenêtre de terminal
    dart pub add --dev bloc_lint:^0.2.0-dev.0
  3. Add an analysis_options.yaml to the root of your project with the recommended rules

    analysis_options.yaml
    include: package:bloc_lint/recommended.yaml
  4. Run the linter

    Fenêtre de terminal
    bloc lint .

That’s all there is to it 🎉

Continue reading for a more in-depth look at configuring and customizing the bloc linter.