Create and Run Dart Console Application Using VSCode? - dart

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?

There is an easy way to create and run a Dart console application:
Open VSCode
Press cmd + shift + p on Mac or ctrl + shift + p on windows.
Choose Dart: New project
4.Then you should select Console Application
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
That's it. I hope you enjoy. 😁

To run a Dart console application:
Install the Code Runner Extension
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.

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.
Press Ctrl + Shift + P on Windows or Cmd + Shift + P on Mac
Select Dart: New Project
Select Console Application
Type in a name, example: hello_world
Hit Enter
Select a folder that already exists.
Wait until it finishes
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.

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

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

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
install brew and npm if you don't have it
install $ npm install -g nodemon (or yarn: $ yarn global add nodemon )
Create dart project.
Create nodemon.json file
{
"watch": [
"lib/" // or any other place where you hold the dart files
],
"execMap": {
"dart": "dart"
},
"ext": "dart"
}
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:
Dart SDK is already installed on your system.
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.
Create Dart console application and save with .dart extension (say Hello.dart), in a suitable location.
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.
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 :

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

Straight Forward Answer
Install Flutter SDK ( this contains dart SDK by default )
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 code
dart sunday.dart the output will be
HAVE A NICE DAY

Have a look at the dcli package. It is an sdk for building console apps in dart.
To create a sample dart console app that demonstrates a number of best practices run
dcli create --template=full myproject

Related

Dart Input in VSCode

How to get input in dart on vscode?
When I click the "Run" button, it only runs the code on the uneditable-terminal, but when I use the "F5" button to run it, as in Python, it brings an error that I need to do some installation and/or configurations, and it auto-creates a "launch.json" file.
please make sure that your dart sdk is installed correctly and then ensure that you install the dart extension for VS code. That should sort you out.
Use cd to get into the directory, then open the terminal and run dart filename.dart.

Testing solution from Exercism

I just installed Exercism and would like to test if I managed to complete the hello-world assignment.
I changed the hello_world.dart file in the lib folder to print the solution, but now I am struggling with running the test file.
In the guide it says to simply run
$ pub run test
Is the $-sign meant to mean something to me? Cause all I am getting is: "'$' is not recognized as an internal or external command,operable program or batch file." I have the same issue with running "pub get" or "pub run test". (What does pub mean?)
To summarize, I ran:
exercism configure "my token"
exercism download --exercise=hello-world --track=dart
cd C:\Users\Martin\exercism\dart\hello-world
I changed the file in C:\Users\Martin\Exercism\dart\hello-world\lib
And then I tried
$ pub run test
pub run test
pub get
And none of these seem to do anything. So, I don't know how to test my file.
I do have flutter installed and run is fine with Android Studio, if that is relevant. It seems to me that I can choose to write the solution in both notepad++ or Android Studio.
Thanks for any help
pub is package manager for dart programming language, just like npm for javascript. Since you've installed it on WINDOWS i'm guessing you used choco - package manager to install the dart-sdk. https://dart.dev/get-dart.
Once installed make sure you've set dart-sdk the path in Environment variable.

Swiftkotlin command line not found for converting Swift to kotlin

I am using Swift kotlin for converting swift code to kotlin which is working on Xcode project but when I am trying to execute from mac terminal it shows SwiftKotlin not found .
Github link: https://github.com/angelolloqui/SwiftKotlin
-bash: swiftkotlin: command not found
Vivek-Mac-mini:~ vivekrawat$
It looks like you need to install the package into
/usr/local/bin/
folder on your Mac, so the terminal can treat it like other command-line commands like cd, open, etc.
From Xcode, run the desired target and copy the generated executables (you can find it under the Products generated folder) in a directory with executable rights for later use. Typically, you could use:
swiftkotlin command line tool: /usr/local/bin/
To do so, you can try the following:
in the left pane of Xcode find the last folder called Products, open it. There should be a file with .app extension.
try to drag the file with .app extension from XCode to your desktop, holding alt key on the keyboard.
open Finder, click Shift+Cmd+G and paste /usr/local/bin/ into the prompt
drag the file from your desktop to that folder
restart the Terminal and try to use the command again

Flutter run command error : FlutterView.send called on a detached view

Can anyone tell me why my app is not installing at all and ending with the error message
Observatory connection never became ready.
the app was working perfectly fine until yesterday .
I have changed the channel to master yesterday in the command prompt and from then on the app never runs.
I even switched back to channel alpha but still no use.
I have attached a screenshot of the command prompt where the installation was stuck forever.
I am not so sure if I need to reinstall the flutter. I am worried if I uninstall the flutter now it may not configure properly and I might break it forever.
Please can someone tell me what exactly might have gone wrong.
Many Thanks,
Mahi
Instead of reinstall try with following: (Mac)
Set Path Variable: via following terminal command
export PATH=~/flutter/bin:$PATH;
that is bin folder path of flutter.
export PATH=[PATH_TO_FLUTTER_GIT_FOLDER]/bin:$PATH
https://flutter.io/setup-macos/#update-your-path
If above not worked. Check with the Dart version
dart --version
Install the latest dart version:
brew install dart --devel
Hope this helps others.
For Mac
open terminal
$ echo $PATH
it will show like this
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:
it means you need to add flutter in to path
Open Finder presss command + shift + G
Put /etc/paths in dialog and press GO
Copy the file "paths" and paste it on desktop
Then open the pasted "paths" file from desktop
and add following line into end of the file "/flutter/bin"
Then copy that file and again paste it on /etc/paths
same like step no 2
it will ask you to replace or not
press replace
Now if you hit $ echo $PATH on terminal it will show
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/swagat/development/flutter/bin
Now your $flutter doctor will work perfectly
The solution is from Google Groups #Flutter-Dev
Thank you very much for your reply #Michael Thomsen.
I've tried what you have suggested and the details are as follows:
When I used flutter run -v command the command prompt output is as follows:
It was stuck for a while at waiting for port connection if I am not wrong.
So I've used AndroidStudio, File>Open and selected android/ folder to run the app then the gradle was showing an error:
Error:Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (25.2.0) and test app (25.4.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
I modified the build.gradle(Module:app) to show the correct version of 25.2.0 which was previously showing 25.4.0.
dependencies {
androidTestCompile 'com.android.support:support-annotations:25.2.0'
}
I just saved the changes inside android studio and the gradle sync completed without any errors.
Now I can run the app from inside Android Studio and also from IntelliJ IDE.
Thanks very much Michael this solved my problem.
Reinstalling flutter would be my starting point. If this problem persists, please post the output of $>flutter doctor and a description of what exactly leads to this error.

How to run angulardart application?

I just download andgularjs dart tutorial from angulardart.org . It provides me some tutorials, but I couldn't able to run those tutorials.
I am using webStorm IDE to run those application. when I am trying to run the application, I am getting following error.
enter image description here
I couldn't find package folder with in web root folder in that application.
You probably need to run pub upgrade. WebStorm provides this action in the context menu of the pubspec.yaml file, also if you open the pubspec.yaml file in the WebStorm editor links for pub actions are shown in the top-left corner of the editor. Alternatively you can run pub run from the command line of your project directory if the path is configured properly on your system so the command is found.

Resources