静态方法 run
启动一个进程并以非交互方式运行到完成。进程运行的是 executable,带有指定的 arguments。
建议为 executable 使用绝对路径,因为解析 executable 路径是平台特定的。在 Windows 中,无论是 environment 映射参数中设置的任何 PATH 还是在 workingDirectory 参数中设置的路径,都忽略以解析 executable 路径。
使用 workingDirectory 为进程设置工作目录。注意,在某些平台上,在执行进程之前会更改目录,这可能会对使用可执行文件和参数的相对路径产生影响。
使用 environment 为进程设置环境变量。如果没有设置,则继承父进程的环境。目前仅支持 US-ASCII 环境变量,如果传递了范围之外的代码点的环境变量,则可能发生错误。
如果 includeParentEnvironment 为 true,进程的环境将包括父进程的环境,其中 environment 优先。默认为 true。
如果 runInShell 为 true,则进程将通过系统 shell 创建。在 Linux 和 OS X 上使用 /bin/sh,而在 Windows 上使用 %WINDIR%\system32\cmd.exe。
用于解码 stdout 和 stderr 到文本的编码通过 stdoutEncoding 和 stderrEncoding 控制。默认编码为 systemEncoding。如果使用 null,则不会发生解码,并且 ProcessResult 将保留二进制数据。
返回一个 Future<ProcessResult>,它完成进程运行的结果,即退出代码、标准输出和标准输入。
以下代码使用 Process.run 在 Linux 上对文件 test.dart 中的 main 进行查找。
var result = await Process.run('grep', ['-i', 'main', 'test.dart']);
stdout.write(result.stdout);
stderr.write(result.stderr);
实现
external static Future<ProcessResult> run(
String executable, List<String> arguments,
{String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
Encoding? stdoutEncoding = systemEncoding,
Encoding? stderrEncoding = systemEncoding});