Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and run a Dart project on the command line

Tags:

dart

I wanted to create a Dart project to play around with, but I couldn't figure out how to create one from the command line.

I tried

dart create playground.dart

I also tried

dart playground.dart 

but the code I pasted in had dependency requirements.

like image 304
Suragch Avatar asked Apr 09 '19 21:04

Suragch


People also ask

How do I run the Dart program from the command line?

Run the app To run the app from the command line, use the Dart VM by running the dart run command in the app's top directory: $ cd cli $ dart run Hello world: 42!

How do I create a Dart file?

Go to android studio settings by following this flow. File => Settings => Languages & Frameworks and select Dart check its Sdk path is it correct or not and is the dart support is enabled for your project or not.

What is Dart command?

The dart tool ( bin/dart ) is a command-line interface to the Dart SDK. The tool is available no matter how you get the Dart SDK—whether you download the Dart SDK explicitly or download only the Flutter SDK.


2 Answers

The easiest way to create a new project is to use Stagehand. This is the same that IntelliJ uses when creating a new Dart project.

You can install it with this command

pub global activate stagehand

And then all you have to do to use it is

mkdir playground
cd playground
stagehand <generator-name>

These are the different generator you can use right now:

  • console-full - A command-line application sample.
  • package-simple - A starting point for Dart libraries or applications.
  • server-shelf - A web server built using the shelf package.
  • web-angular - A web app with material design components.
  • web-simple - A web app that uses only core Dart libraries.
  • web-stagexl - A starting point for 2D animation and games.
like image 146
Erly Avatar answered Oct 24 '22 00:10

Erly


Update

Dart now supports creating projects directly. No more need for stagehand.

dart create playground

This creates a folder named playground with a Dart project inside.

Alternate answer

1. Create a new folder

mkdir playground

2. Add a dart file with a main() function

playground.dart

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

3. Run the app

dart playground.dart

If your app has dependencies...

4. Setup configuration

Create a pubspec.yaml file

touch pubspec.yaml

Paste in any dependencies that you have

name: playground
description: Just a place to practice
version: 0.0.1

dependencies:
  crypto: ^2.0.6

5. Get dependencies

pub get
like image 43
Suragch Avatar answered Oct 24 '22 02:10

Suragch