prefer_relative_imports
Prefer relative imports for files in lib/
.
Details
#PREFER 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 relative imports for files within the
lib/
directory.
BAD:
import 'package:my_package/bar.dart';
GOOD:
import 'bar.dart';
Incompatible rules
#The prefer_relative_imports
rule is incompatible with the following rules:
Enable
#To enable the prefer_relative_imports
rule,
add prefer_relative_imports
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- prefer_relative_imports
If you're instead using the YAML map syntax to configure linter rules,
add prefer_relative_imports: true
under linter > rules:
linter:
rules:
prefer_relative_imports: true
除非另有说明,文档之所提及适用于 Dart 3.7.0 版本,本页面最后更新时间: 2025-01-27。 查看文档源码 或者 报告页面问题。