Is there a way to start Dart Future synchronously?

How can I get results in the future? For instance:

void main() {
  Process.run('some_shell_command', []).then((ProcessResult result) {
    print(result.stdout); // I have the output here...
  });
  // ... but want it here.
}
+5
source share
3 answers

supportawait is in experimental condition and can be used as:

void main() async {
  ProcessResult result = await Process.run('some_shell_command', []);
  print(result.stdout); // I have the output here...
}
+5
source

Sorry, this is simply not possible.

There are times when a function returns new Future.immediate(value)and maybe you can get a value, but:

  • This is not one of these cases. Processes run completely asynchronously using the VM.
  • The libv2 update has removed the ability to directly access the direct value.

, , Process.run(), , , , , , main(). , , - , .

Async , Dart Javascript, . , , , .., .

+2

.

acync API, , async , .

, , then()

void main() {
  Process.run('some_shell_command', []).then(doSomethingWithResult);  
}

void doSomethingWithResult(result) {
   print(result.stdout); // I have the output here...
}
+2

All Articles