Skip to main content

avoid-banned-imports

added in: 1.6.0
⚙️🛠
Pro+

Warns against using banned imports.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set entries (default is empty) to configure the list of entries for imports to ban.

Each entry is an object with 4 fields (paths, deny, message and severity):

  • Set paths (can be a regular expression, optional) to configure the list of paths the entry will apply to. If unset, applies to all files.
  • Set deny (can be a regular expression) to configure the list of imports to ban.
  • Set message to configure a user-facing message for each issue created from this config entry.
  • Set severity (optional) to override default severity for the given entry.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-banned-imports:
entries:
- paths: ['some/folder/.*\.dart', 'another/folder/.*\.dart']
deny: ['package:flutter/material.dart']
message: 'Do not import Flutter Material Design library, we should not depend on it!'
severity: error
- paths: ['core/.*\.dart']
deny: ['package:flutter_bloc/flutter_bloc.dart']
message: 'State management should be not used inside "core" folder.'
warning

For Windows devices, ensure that the paths config works with Windows path separators.

Example

❌ Bad:

// LINT: This import is not allowed (Do not import Flutter Material Design library, we should not depend on it!).
import "package:flutter/material.dart";

// LINT: This import is not allowed (State management should be not used inside "core" folder.).
import "package:flutter_bloc/flutter_bloc.dart";

✅ Good:

// Correct, no banned imports in the listed folders