已执行dart pub outdated等命令,为何dart migrate仍提示未迁移依赖?
问题描述
我执行了以下命令:
> dart pub outdated --mode=null-safety Showing dependencies that are currently not opted in to null-safety. [✗] indicates versions without null safety support. [✓] indicates versions opting in to null safety. All your dependencies declare support for null-safety. > dart pub upgrade --null-safety No dependencies changed. 1 package is discontinued. 148 packages have newer versions incompatible with dependency constraints. Try `dart pub outdated` for more information. No changes to pubspec.yaml! > dart pub get
随后运行dart migrate,得到如下输出:
Analyzing project... [---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\]Bad state: Error: package has unmigrated dependencies. Before migrating your package, we recommend ensuring that every library it imports (either directly or indirectly) has been migrated to null safety, so that you will be able to run your unit tests in sound null checking mode. You are currently importing the following non-null-safe libraries: (a list of files of my project like: package:opengames/presentation/widgets/not_found_screen.dart) Please upgrade the packages containing these libraries to null safe versions before continuing. To see what null safe package versions are available, run the following command: `dart pub outdated --mode=null-safety`. To skip this check and try to migrate anyway, re-run with the flag `--skip-import-check`.
为何会出现这种情况?我已经执行过dart pub outdated --mode=null-safety命令了。
原因分析
dart pub outdated --mode=null-safety仅检查第三方依赖包是否支持空安全,而dart migrate报错中列出的是你自己项目内的代码文件——这些文件不属于第三方依赖范畴,所以之前的命令不会对它们进行空安全检查。
报错的核心问题是:你项目里的这些本地文件还未完成空安全迁移,比如代码中仍存在未标注空值的类型、使用了旧的非空安全语法等,导致迁移工具检测到未迁移的本地库。
解决方法
- 优先处理本地代码的空安全迁移:逐个修改报错列表中的本地文件,按照Dart空安全规范补充类型标注、处理空值场景,确保每个文件都符合空安全要求后再执行
dart migrate。 - 若想跳过本地文件检查直接尝试迁移(不推荐,可能引发后续空安全错误),可以执行命令:
dart migrate --skip-import-check
内容的提问来源于stack exchange,提问作者João Abrantes
相关产品推荐
相关产品推荐

