Customizing Lint Rules
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.
Enabling and Disabling Rules
Section titled “Enabling and Disabling Rules”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).
Enabling Recommended Rules
Section titled “Enabling Recommended Rules”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:
dart pub add --dev bloc_lintThen edit your analysis_options.yaml to include the rule set:
include: package:bloc_lint/recommended.yamlEnabling Individual Rules
Section titled “Enabling Individual Rules”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:
bloc: rules: - avoid_flutter_imports - avoid_public_bloc_methodsDisabling Individual Rules
Section titled “Disabling Individual Rules”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:
include: package:bloc_lint/recommended.yamlbloc: rules: avoid_public_bloc_methods: false prefer_bloc: trueCustomizing Rule Severity
Section titled “Customizing Rule Severity”You can adjust the severity of any rule like so:
bloc: rules: avoid_flutter_imports: infoNow the same lint rule will be reported with a severity of info instead of warning:
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:
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 foundAnalyzed 1 fileThe supported severity options are:
| Severity | Description |
|---|---|
error | Indicates the pattern is not allowed. |
warning | Indicates the pattern is suspicious but allowed. |
info | Provides information to users but is not a problem |
hint | Proposes a better way of achieving a result. |
Excluding Files
Section titled “Excluding Files”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:
include: package:bloc_lint/recommended.yamlanalyzer: exclude: - "**.g.dart"Ignoring Rules
Section titled “Ignoring Rules”Just like with official Dart lint rules, you can ignore bloc lint rules for a given file or line of code using // ignore_for_file and // ignore respectively.
Ignoring Lines
Section titled “Ignoring Lines”We can ignore specific occurrences of rule violations by adding an ignore comment either right above the offending line or by appending it to the offending line.
For example, we can ignore specific occurrences of prefer_file_naming_conventions in a given file:
import 'package:bloc/bloc.dart'; class CounterCubit extends Cubit<int> {} // ignore: prefer_file_naming_conventions enum CounterEvent { increment, decrement } // ignore: prefer_file_naming_conventionsclass CounterBloc extends Bloc<CounterEvent, int> {}Ignoring Files
Section titled “Ignoring Files”We can ignore all occurrences of rule violations within a file by adding an ignore_for_file comment anywhere in the file.
For example, we can ignore all occurrences of prefer_file_naming_conventions in a given file:
// ignore_for_file: prefer_file_naming_conventionsimport 'package:bloc/bloc.dart'; class CounterCubit extends Cubit<int> {} enum CounterEvent { increment, decrement } class CounterBloc extends Bloc<CounterEvent, int> {}