flutter - 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],
);
解决方案
您可能在这里找到了解决方案: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 中替换它
推荐阅读
- c++ - 带有clang -O3优化的ARM(Apple M1)上的模运算错误结果
- javascript - 创建一个带有 keyName 和 value 输入的对象,它们都位于不同的父 jsx 元素中
- bootstrap-4 - 使用角度材料自动完成文本框时底部的文本显示?
- python - 为什么 SQLAlchemy Join 返回空数据?
- ios - Info.plist 中键字符串的大小限制是多少?
- python - 在 Jupyter notebook 中打印一个汉字(Python 3)
- awk - 如何生成一个字符重复n次的字符串?
- c# - 当检查元素显示正确的 href 时,cshtml 中的 URL 操作不会呈现正确的 URL,它会转到同一控件中的另一个操作
- intel - 在计算总和时了解管道不同层/阶段的效率
- google-sheets - ReferenceError: calendarId 未定义