Skip to content

max_imports

v1.0.0WarningConfigurableCode Quality

This rule flags a file that imports more libraries than the configured budget.

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:

report_screen.dart
import 'dart:async';
import 'dart:convert';
class ReportScreen {}
report_encoder.dart
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'dart:developer';
class ReportEncoder {}

export directives do not count, so a barrel of forty exports stays silent under the default budget:

// widgets.dart — 0 imports counted
export 'buttons.dart';
export 'cards.dart';
export 'dialogs.dart';

Set count_exports: true if you want a barrel held to the same budget.

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
max_imports:
enabled: true
analysis_options.yaml
many_lints:
rules:
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

To disable this rule:

many_lints.yaml
rules:
max_imports: false

To keep the rule on but skip certain paths, use per-rule exclude.