max_imports
This rule flags a file that imports more libraries than the configured budget.
Why use this rule
Section titled “Why use this rule”The import count measures how much a file depends on. A file importing twenty libraries is coupled to twenty things that can change under it, and usually has taken on more than one job.
This rule is in the pedantic preset.
With max_imports: 5, a screen file that grew into a service layer:
import 'dart:async';import 'dart:convert';import 'dart:io';import 'dart:math';import 'dart:typed_data';import 'dart:developer'; // LINT: 6 imports, over the limit of 5
class ReportScreen {}The diagnostic lands on the first directive, so it points at the import block rather than at whichever import happened to push it over.
Split the file so each part imports only what it needs:
import 'dart:async';import 'dart:convert';
class ReportScreen {}import 'dart:io';import 'dart:math';import 'dart:typed_data';import 'dart:developer';
class ReportEncoder {}Barrel files are not reported
Section titled “Barrel files are not reported”export directives do not count, so a barrel of forty exports stays silent under the default budget:
// widgets.dart — 0 imports countedexport 'buttons.dart';export 'cards.dart';export 'dialogs.dart';Set count_exports: true if you want a barrel held to the same budget.
Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: max_imports: enabled: trueOptions
Section titled “Options”many_lints: rules: max_imports: max_imports: 15 count_exports: falserules: max_imports: max_imports: 15 count_exports: false| Option | Type | Default | Description |
|---|---|---|---|
max_imports |
int | 15 |
How many libraries a file may import |
count_exports |
bool | false |
Whether export directives count toward the budget |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: max_imports: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_long_files— Keep a file within a line budget.avoid_long_parameter_list— Keep parameter lists within a budget.avoid_too_many_methods— Keep a class within a method budget.max_statements— Keep a function within a statement budget.