ZetCode

Dart process

last modified January 28, 2024

In this article we show how to execute external programs in Dart language.

The Process class is used to run external programs. To run processes, we can use the run, runAsync, and start member functions.

The run function executes the process non-interactively to completion, while the start funcion allows your code to interact with the running process.

Dart process simple example

In the first example, we execute an ls command.

main.dart
import 'dart:io';

void main() async {
  var result = await Process.run('ls', ['-l']);
  print(result.stdout);
}

The program executes the ls command with Process.run.

var result = await Process.run('ls', ['-l']);

The function takes the name of the program as its first parameter. The second parameter is a list of program arguments. The function returns a Future<ProcessResult>, which we await.

print(result.stdout);

We print the result of the executed command to the terminal.

$ dart main.dart
total 7
-rw-r--r-- 1 Jano 197121 182 Feb  1 16:19 first.dart
-rw-r--r-- 1 Jano 197121 327 Feb  1 16:21 main.dart
-rw-r--r-- 1 Jano 197121 266 Feb  1 16:28 open.dart
-rw-r--r-- 1 Jano 197121 187 Feb  1 16:30 run_in_shell.dart
-rw-r--r-- 1 Jano 197121 152 Feb  1 16:23 simple.dart
-rw-r--r-- 1 Jano 197121 201 Feb  1 16:39 stream.dart
-rw-r--r-- 1 Jano 197121  33 Feb  1 16:27 words.txt

Dart process exit code

The exit code of a program tells the caller how to program has executed. Traditionally, a 0 exit code means that the program has run successfully and -1 unsuccessfully. The returned numbers can indicate specific error messages.

main.dart
import 'dart:io';

void main() async {
  var process = await Process.start('echo', ['an old falcon']);
  var res = await process.stdout.first;
  print(res);
  print(utf8.decode(res));
  var exitCode = await process.exitCode;
  print('exit code: $exitCode');
}

In the program, we run the echo command with Process.start, show its output and exit code.

var process = await Process.start('echo', ['an old falcon']);

The Process.start starts a process running the executable with the specified arguments. It returns a Future<Process> that completes with a process instance when the process has been successfully started.

var res = await process.stdout.first;

We get the first element of the stream. We stop listening to this stream after the first element has been received. We receive a list of bytes.

print(res);
print(utf8.decode(res));

We print the list of bytes and then we print the decoded text.

var exitCode = await process.exitCode;
print('exit code: $exitCode');

We read and print the exit code. The echo command retuns 0 if it has run successfully.

$ dart main.dart
[97, 110, 32, 111, 108, 100, 32, 102, 97, 108, 99, 111, 110, 10]
an old falcon

exit code: 0

Dart process standart input

In the next program, we read some data from the standard input.

main.dart
import 'dart:io';
import 'dart:convert';

void main() async {
  var process = await Process.start('cat', []);
  process.stdout.transform(utf8.decoder).forEach(print);
  process.stdin.writeln('an old falcon');
  process.stdin.writeln('a stormy night');
  process.stdin.writeln('a black cat');

  process.stdin.close();
}

The program executes a cat command and reads some data from the standard input for the command.

$ dart main.dart
an old falcon
a stormy night
a black cat

Dart run and kill process

The process can be terminated with the kill member function.

main.dart
import 'dart:io';

void main() async {
  var process = await Process.start('notepad.exe', []);

  sleep(Duration(seconds: 3));

  process.kill();
}

We run notepad application, sleep for three seconds and then kill the program with kill.

Dart run process via callbacks

In the following example, we run notepad and pass it a text file as argument.

main.dart
import 'dart:io';

void main() {
  Process.run('notepad.exe', ['words.txt']).then((ProcessResult rs) {
    print(rs.exitCode);
    print(rs.pid);
    print(rs.stdout);
    print(rs.stderr);
  });
}

We don't use async/await; this time we use the then callback.

Source

Dart Process - language reference

In this article we have showed how to execute external programs in Dart.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Dart tutorials.