prefer-named-imports
Warns if the configured import is not a named import.
Standardizing named imports across a codebase improves readability (for example, if you want all imports from math
to have a prefix, this rule will help ensure that happens in every case) and consistency.
note
This rule requires configuration in order to highlight any issues.
⚙️ Config
Set entries
(default is empty) to configure the list of named import entries.
Each entry is either a map or a string:
- If the entry is a map, the key represents the imported package name and the value is the expected name alias.
- If the entry is a string, the value represents both the imported package name and the expected name alias.
analysis_options.yaml
dart_code_metrics:
rules:
- prefer-named-imports:
entries:
- test
- path: p
- "dart:io": io
Example
With the configuration in the example above, here are some bad/good examples.
❌ Bad:
import 'package:test/test.dart'; // LINT: This import should be named 'as test'.
import 'package:some/other.dart';
import 'package:path/path.dart'; // LINT: This import should be named 'as p'.
✅ Good:
import 'package:test/test.dart' as test;
import 'package:some/other.dart';
import 'package:path/path.dart' as p;