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

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

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.

Tags:

Dart