Skip to content

Customizing Lint Rules

This content is not available in your language yet.

You can customize the behavior of the bloc linter by changing the severity of individual rules, individually enabling or disabling rules, and excluding files from static analysis.

The bloc linter supports a growing list of lint rules. Note that lint rules don’t have to agree with each other. For example, some developers might prefer to use blocs (prefer_bloc) while others might prefer to use cubits (prefer_cubit).

The bloc library provides a set of recommended lint rules as part of the bloc_lint package.

To enable the recommended set of lints add the bloc_lint package as a dev dependency:

Terminal window
dart pub add --dev bloc_lint:^0.2.0-dev.0

Then edit your analysis_options.yaml to include the rule set:

analysis_options.yaml
include: package:bloc_lint/recommended.yaml

To enable individual rules, add bloc: to the analysis_options.yaml file as a top-level key and rules: as a second-level key. On subsequent lines, specify the rules you want as a YAML list (prefixed with dashes).

For example:

analysis_options.yaml
bloc:
rules:
- avoid_flutter_imports
- avoid_public_bloc_methods

If you include an existing rule set such as the recommended set, you may want to disable one or more included lint rules. Disabling rules is similar to enabling them, but requires the use of a YAML map rather than a list.

For example, the following includes the recommended set of lint rules except for avoid_public_bloc_methods and additionally enables the prefer_bloc rule:

analysis_options.yaml
include: package:bloc_lint/recommended.yaml
bloc:
rules:
avoid_public_bloc_methods: false
prefer_bloc: true

You can adjust the severity of any rule like so:

analysis_options.yaml
bloc:
rules:
avoid_flutter_imports: info

Now the same lint rule will be reported with a severity of info instead of warning:

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

The output of the bloc lint command should look like:

Terminal window
info[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

The supported severity options are:

SeverityDescription
errorIndicates the pattern is not allowed.
warningIndicates the pattern is suspicious but allowed.
infoProvides information to users but is not a problem
hintProposes a better way of achieving a result.

Sometimes it’s okay for static analysis to fail. For example, you might want to ignore warnings or errors reported in generated code that wasn’t written by you and your team. Just like with official Dart lint rules, you can use the exclude: analyzer option to exclude files from static analysis.

You can either list individual files or use glob patterns.

For example, we can exclude all generated Dart code via the following analysis options:

analysis_options.yaml
include: package:bloc_lint/recommended.yaml
analyzer:
exclude:
- "**.g.dart"