1

Is it possible to run this code in Android studio without firing up any device, I just want to print it to the console.

void main() {
  print("Hello World");
}

Note:

I am not looking for DartPad or any other online IDE to run Dart code.

  • https://stackoverflow.com/questions/50977917/how-to-start-dart-project-in-android-studio –  Oct 16 '19 at 08:36

2 Answers2

2

in your Dart file right click on the green arrows in front of your main() function:

enter image description here

then click on the dart icon from the popup menuCreate ...

enter image description here

and then OK:

enter image description here

now you can run it without the simulator:

enter image description here

And get the console output in the Run window:

enter image description here

Jean G
  • 147
  • 1
  • 10
1

On a Unix-like system (e.g. Linux, macOS), if dart is in your executable PATH, you can add a shebang line to your .dart file:

#!/usr/bin/env dart

void main() {
  print("Hello World");
}

and mark your file executable (chmod a+x your_file.dart). Then you can run your_file.dart directly without needing to run dart your_file.dart explicitly.

(If the dart binary is not in PATH, then you would need to use #!/full/path/to/dart as the first line instead.)

On Windows, you could set up a file association for .dart files.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204