Launch VSCode from a Flutter Windows App

2021-03-21 21:08 (3 years ago) ytyng
Process.run('code.cmd', ['<path-to-file-you-want-to-open>']);

Points to consider: When you install Visual Studio Code, you will be asked if you want to add it to the PATH. If you turn it ON, you will be able to start VSCode with the code command.

However, the correct command is actually code.cmd. Just code will not work when launching from Flutter's Process.run, so you need to include .cmd.

That's all.


Below is a previous workaround that seemed cumbersome but was kept as a memo because it might still be meaningful. Not recommended.

I wanted to launch VSCode from a Windows app made with Flutter.

if (Platform.isWindows) {
  var r = await Process.run('where.exe', ['code']);
  if (r.exitCode != 0 || r.stdout == '') {
    return;
  }
  var l = r.stdout.split('\r\n');
  Process.run(l[1], ['<path-to-file-you-want-to-open>']);
}

It's a bit forced, but this is how I did it.

where.exe works the same way as which on macOS or Linux. Error handling should be done more properly, but in this case, since the users are limited, I did it roughly like this.

The ProcessResult instance resulting from Process.run will have exitCode == 0 if the command completes successfully. If it fails, it will be something other than 0, probably 1.

However, I didn't use the exitCode for determination this time.

The command result is stored in the stdout of the ProcessResult. In the case of Windows, the newline code is \r\n.

The result of where.exe will output the full path without extension on the first line and with the extension on the second line. To execute with Process.run, specify the one with the extension.

Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011