depend_on_referenced_packages
The imported package '{0}' isn't a dependency of the importing package.
Description
#The analyzer produces this diagnostic when a package import refers to a
package that is not specified in the pubspec.yaml
file.
Depending explicitly on packages that you reference ensures they will always exist and allows you to put a dependency constraint on them to guard against breaking changes.
Example
#Given a pubspec.yaml
file containing the following:
dependencies:
meta: ^3.0.0
The following code produces this diagnostic because there is no dependency
on the package a
:
import 'package:a/a.dart';
Common fixes
#Whether the dependency should be a regular dependency or dev dependency
depends on whether the package is referenced from a public library (one
under either lib
or bin
), or only private libraries, (such as one
under test
).
If the package is referenced from at least one public library, then add a
regular dependency on the package to the pubspec.yaml
file under the
dependencies
field:
dependencies:
a: ^1.0.0
meta: ^3.0.0
If the package is referenced only from private libraries, then add a
dev dependency on the package to the pubspec.yaml
file under the
dev_dependencies
field:
dependencies:
meta: ^3.0.0
dev_dependencies:
a: ^1.0.0
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。