Create and Run Dart Console Application Using VSCode?

I can create Flutter new application using VSCode. Now I want to learn Dart programming language.

How can I create and run a Dart console application Using VS Code?

12 Answers

There is an easy way to create and run a Dart console application:

  1. Open VSCode

  2. Press cmd + shift + p on Mac or ctrl + shift + p on windows.

  3. Choose Dart: New project

enter image description here

4.Then you should select Console Application

enter image description here

5.After locating console application everything is ready for you 😊

Well, how to run it? So easy!!!🙂

Just go to terminal and type: "dart main.dart"

or copy the main.dart path then type: "dart (paste main file path)"

or press ctrl + F5

enter image description here

That's it. I hope you enjoy. 😁

3

To run a Dart console application:

  1. Install the Code Runner Extension
  2. Open the Dart code file in Text Editor, then use shortcut Ctrl+Alt+N, or right click the Text Editor and then click Run Code in context menu, the Dart console application will run and the output will be shown in the Output Window.
0

Steps to run in VS Code:

  • Open Visual Studio Code.
  • Close whatever folder you have open.
  • Make sure you have Dart Code Extension installed, or install it if you don't have it.

enter image description here

  • Press Ctrl + Shift + P on Windows or Cmd + Shift + P on Mac

  • Select Dart: New Project

enter image description here

  • Select Console Application

enter image description here

  • Type in a name, example: hello_world
  • Hit Enter
  • Select a folder that already exists.
  • Wait until it finishes

enter image description here

  • main.dart file will show up.

  • Hit Ctrl + F5 to run or Run, Run without debugging.

  • In the Debug Console window, you will see the result.

enter image description here

Dart SDK installing with Flutter. For MacOS Dart directory /Users/burak/flutter/bin/cache/dart-sdk/bin

Add to PATH this bin directory and use dart with below.

dart --version

Run .dart file.

dart main.dart
2

I found a way to do it, you can install packages etc as well. instructions below..

- Installing

pub global activate stagehand

- Add to .bash_profile

export PATH="$PATH":"$HOME/.pub-cache/bin"

- Create and download packages

mkdir fancy_project

cd fancy_project
stagehand console-full
pub get
1

If you are not only flutter mobile developer, but web-developer too, you are probably using npm or yarn. If so you can use nodemon to run your dart code automatically after any change

  1. install brew and npm if you don't have it

  2. install $ npm install -g nodemon (or yarn: $ yarn global add nodemon )

  3. Create dart project.

  4. Create nodemon.json file

     { "watch": [ "lib/" // or any other place where you hold the dart files ], "execMap": { "dart": "dart" }, "ext": "dart" }
  5. Run you code: $ nodemon ./lib/main.dart

p.s. Code in the .dart file should have main function:

void main() { print('hello world');
}

If you are using VsCode, just save code by CMD + S and nodemon demon will run updated code,

If you already have flutter (windows) installed , then find where you installed flutter. Once you are in flutter folder then go through

flutter\bin\cache\dart-sdk\bin .

I have installed flutter on C:\src folder so my path is

C:\src\flutter\bin\cache\dart-sdk\bin .

Now copy this full path and set it as environment variable. If you can't set environment variable then open search bar and type "env" . Click the result click environment variable and add that path in( Path variable).

In VScode Ctrl+Shift+` this will open a new terminal . Right click on your dart code and copy path.

In terminal just type this line without bracket and press enter [dart "paste path"]

If you have already run flutter application on VSCode, then you should be able to run dart console applications. However, please check the following:

  1. Dart SDK is already installed on your system.
  2. Check if the Dart 2.20 Extension (Current Version) is already installed on the VSCode. If not, please install the same on your VSCode, and restart VSCode.
  3. Create Dart console application and save with .dart extension (say Hello.dart), in a suitable location.
  4. From the top menu bar of VSCode, open a terminal. From the terminal inside of the VSCode, change to directory where you have saved the dart program.
  5. Run the dart console program with the command:

    dart [hello.dart]

Console program results are shown in the terminal. I hope this is what you are looking for.

You can insert this JSON code in your setting.json in the .vscode folder .

"dart.sdkPath": "[DART_SDK_PATH]"

For example in my Windows OS :

"dart.sdkPath": "F:\\dev\\sdk\\dart-sdk"

And My All Setting in setting.json :

{ "version": "0.2.0", "configurations": [ { "name": "Dart", "program": "bin/main.dart", "request": "launch", "type": "dart" } ] , "dart.sdkPath": "F:\\dev\\sdk\\dart-sdk"
}

Notice the This Image :

Dart Setup in vscode

Have a look at the dcli package. It is an sdk for building console apps in dart.

To run dart hello world from VSCODE

boom that's it. it will print value in console.

Straight Forward Answer

  1. Install Flutter SDK ( this contains dart SDK by default )
  2. Add dart to you environment path

Now create a dart file and run it with the dart command, for example create sunday.dart file and put the following content inside.

void main(){ print("HAVE A NICE DAY")
}

run the dart code with the following codedart sunday.dart the output will be

HAVE A NICE DAY

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like