首页 > 解决方案 > Dart在使用Process.run时如何隐藏cmd?

问题描述

构建 Dart 应用程序后,函数 Process.run 开始打开一个可见的 cmd 用于第二个二。

final bool checkEnvironment = environment == ShellEnvironment.powershell;
ProcessResult result = await Process.run(
  checkEnvironment ? 'powershell' : command,
  checkEnvironment ? [command] : args,
  runInShell: checkEnvironment,
);

示例链接(gif):https ://imgur.com/jSPN6ew对于每个命令,它都会打开一个新的 cmd 窗口。

如果我用想法(不是构建版本)启动应用程序 - 这种事情不会发生

也试过这个版本 - 仍然是同样的问题:

final bool checkEnvironment = environment == ShellEnvironment.powershell;
ProcessResult result = await Process.run(
  'start',
   checkEnvironment ? ['/min', 'powershell', '-command', command] : ['/min', 'cmd', '/c', command],
   runInShell: true,
);

发现一篇runInShell创建新窗口的文章,所以我删除了它,但结果还是一样。

final bool checkEnvironment = environment == ShellEnvironment.powershell;
ProcessResult result = await Process.run(
  checkEnvironment ? 'powershell.exe' : 'cmd',
  checkEnvironment ? ['-command', command] : ['/c', command],
);

标签: flutterdart

解决方案


您可能在这里找到了解决方案:https ://github.com/flutter/flutter/issues/47891 。但不要使用它!它不适用于窗口 7,并且在关闭应用程序后用户无法删除应用程序时会产生问题,因为 CMD 的一个实例仍在运行。

试试这种方法

// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  CreateAndAttachConsole();
} else {
  AllocConsole();
  ShowWindow(GetConsoleWindow(), SW_HIDE);
}

您可以在 windows/runner/main.cpp 中替换它


推荐阅读