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

使用dcli的Dart CLI应用执行cd命令报错,无法运行flutter pub get

解决dcli执行cd命令报错:找不到cd可执行文件

问题原因

cd是Shell的内置命令,并非独立的可执行程序。dcli的run方法会尝试在系统PATH中查找对应可执行文件,因此会抛出"Could not find cd on the path"错误。此外,即使cd能执行,每个run调用都是独立进程,cd不会改变后续命令的工作目录。

原错误代码

void _runFlutterPubGet(String path) {
  'cd $path'.run;
  'flutter pub get'.run;
}

错误日志

Unhandled exception:
cd C:\Users\baran\Software\Self\use_template\bin\trial_app 
exit: 2
reason: Could not find cd on the path.
wait_for_ex.dart : waitForEx : 21
runnable_process.dart : RunnableProcess._waitForStart : 300
runnable_process.dart : RunnableProcess.start : 278
runnable_process.dart : RunnableProcess.run : 167
run.dart : start : 249
string_as_process.dart : StringAsProcess.run : 80
use_template_base.dart : UseTemplateBase._runFlutterPubGet : 281
use_template_base.dart : UseTemplateBase.exec : 235
use_template.dart : main : 45
isolate_patch.dart : _delayEntrypointInvocation.<anonymous closure> : 295
isolate_patch.dart : _RawReceivePortImpl._handleMessage : 192

解决方案

方案1:指定命令的工作目录(推荐)

直接通过dcli的run方法的workingDirectory参数指定执行路径,无需执行cd:

void _runFlutterPubGet(String path) {
  'flutter pub get'.run(workingDirectory: path);
}

这种方式不依赖Shell,跨平台兼容性更好,且每个命令的工作目录明确。

方案2:在同一Shell进程中执行多命令

将cd和flutter pub get合并为一条Shell命令,通过&&连接,确保在同一进程中执行:

void _runFlutterPubGet(String path) {
  'cd "$path" && flutter pub get'.run;
}

注意给路径添加双引号,避免路径含空格时出错。这种方式适合需要连续执行多个Shell内置命令的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 10:45:32