You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter空安全迁移遇unmigrated dependencies错误的排查与解决

问题

我有一个规模较大的Flutter项目,目前正在添加空安全支持。项目分为“core”和“client”两个模块,其中“client”依赖“core”模块。

项目的pubspec.yaml配置如下:

environment:
   sdk: ">=2.10.0 <3.0.0"
..... 
 app_core:
   path: ../core

我首先执行dart migrate命令完成了core模块的迁移,再次运行该命令时提示:

All sources appear to be already migrated.  Nothing to do.

接下来我尝试迁移client模块,先执行dart pub outdated --mode=null-safety,结果显示所有依赖均已支持空安全;随后执行dart pub upgrade --null-safety,提示有1个依赖变更,但pubspec.yaml无修改;再执行dart pub get后,运行dart migrate时出现如下错误:

Bad state: Error: package has unmigrated dependencies.

Before migrating your package, we recommend ensuring that every library it
(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:
...//file list
..............
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`.

错误中列出的文件均来自client模块,且这些文件本身无语法错误。请问该错误产生的原因是什么?如何解决?

原因分析
  • core模块的空安全状态未被client模块识别:虽然core模块已完成迁移,但它的pubspec.yaml仍使用旧的非空安全SDK范围(>=2.10.0 <3.0.0),dart工具链无法确认其已适配空安全,因此将其判定为client模块的非空安全依赖,进而牵连client模块自身未迁移的文件被误报。
  • client模块的SDK版本未更新:client模块的环境SDK版本仍停留在>=2.10.0 <3.0.0,未达到空安全要求的最低版本(>=2.12.0),导致migrate工具认为项目整体不满足空安全前置条件。
解决方法

1. 更新core模块的pubspec.yaml

进入core模块目录,修改environment部分的SDK范围:

environment:
  sdk: ">=2.12.0 <3.0.0"

执行dart pub get使配置生效。

2. 更新client模块的pubspec.yaml

  • 修改environment部分的SDK范围为:
environment:
  sdk: ">=2.12.0 <3.0.0"
  • 重新执行dart pub upgrade --null-safety,确保core模块以空安全模式被client依赖。

3. 重新执行迁移命令

在client模块目录下运行dart pub get,之后再执行dart migrate,此时工具会正确识别core模块的空安全状态,正常处理client模块的迁移流程。

临时备选方案

如果上述步骤仍有问题,可以临时使用dart migrate --skip-import-check跳过依赖检查,直接进入client模块的迁移,但不推荐长期使用,可能引发后续空安全隐性问题。

内容的提问来源于stack exchange,提问作者FetFrumos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:00:59