Flutter pub get报错:相对路径不适用于非本地pubspec依赖
问题解决:Flutter Git依赖中本地Path引用导致的pub get报错
问题原因
你在cdm_flutter_api的pubspec.yaml里用相对路径./lib/CdmPlugin/引用本地依赖plugincdm,这种配置仅在cdm_flutter_api作为本地项目运行时有效。当其他项目通过Git引用cdm_flutter_api时,pub无法解析这个相对路径——因为Git拉取的依赖是独立的缓存副本,该相对路径要么指向不存在的资源,要么pub禁止在远程依赖中使用本地path引用。
解决方案
方案1:将plugincdm转为独立Git依赖
把plugincdm上传到独立的Git仓库,修改cdm_flutter_api的pubspec.yaml,改用Git方式引用:
dependencies: plugincdm: git: url: https://github.com/example/plugincdm.git ref: 你的分支/标签/commit哈希
这样其他项目拉取cdm_flutter_api时,pub会自动从Git拉取plugincdm的代码。
方案2:合并plugincdm到cdm_flutter_api包内
如果plugincdm是cdm_flutter_api的内部模块,直接把它的代码合并到cdm_flutter_api的包结构中,删除path依赖配置,将其作为包的一部分使用。
方案3:使用Git子模块(需额外操作)
在cdm_flutter_api仓库中添加plugincdm作为Git子模块:
git submodule add https://github.com/example/plugincdm.git lib/CdmPlugin
然后提交子模块配置到Git。其他项目拉取cdm_flutter_api时,需要手动初始化子模块:
git submodule update --init --recursive
不过这种方式需要使用者额外执行子模块初始化,不如方案1便捷。
内容的提问来源于stack exchange,提问作者Hugo Fertl
相关产品推荐
相关产品推荐

