always_use_package_imports
Avoid relative imports for files in lib/
.
Details
#DO avoid relative imports for files in lib/
.
When mixing relative and absolute imports it's possible to create confusion
where the same member gets imported in two different ways. One way to avoid
that is to ensure you consistently use absolute imports for files within the
lib/
directory.
This is the opposite of 'prefer_relative_imports'.
You can also use 'avoid_relative_lib_imports' to disallow relative imports of
files within lib/
directory outside of it (for example test/
).
BAD:
import 'baz.dart';
import 'src/bag.dart'
import '../lib/baz.dart';
...
GOOD:
import 'package:foo/bar.dart';
import 'package:foo/baz.dart';
import 'package:foo/src/baz.dart';
...
Incompatible rules
#The always_use_package_imports
rule is incompatible with the following rules:
Enable
#To enable the always_use_package_imports
rule,
add always_use_package_imports
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- always_use_package_imports
If you're instead using the YAML map syntax to configure linter rules,
add always_use_package_imports: true
under linter > rules:
linter:
rules:
always_use_package_imports: true
除非另有说明,文档之所提及适用于 Dart 3.7.0 版本,本页面最后更新时间: 2025-01-27。 查看文档源码 或者 报告页面问题。