title | description | nextpage | prevpage | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Get started: command-line and server apps |
Get Dart, run and compile a small app |
|
|
Follow these steps to start using the Dart SDK to develop command-line and server apps. First you’ll play with the Dart language and libraries in your browser, no download required. Then you’ll install the Dart SDK, write a small program, and run that program using the Dart VM. Finally, you'll use an AOT (ahead of time) compiler to compile your finished program to native machine code, which you'll execute using the Dart runtime.
With DartPad you can experiment with the Dart language and APIs, no download necessary.
For example, here's an embedded DartPad that lets you play with the code for a small Hello World program. Click run {% asset red-run.png alt="" %} to run the app; the console output appears beneath the code. Try editing the source code—perhaps you'd like to change the greeting to use another language. To get the full DartPad experience, open the example at dartpad.dartlang.org.
<iframe src="{{site.custom.dartpad.embed-inline-prefix}}?id=27e044ec9e2957d9c5c7062871ce8bf3&verticalRatio=70" width="100%" height="200px" style="border: 1px solid #ccc;"> </iframe>More information:
{% include get-sdk.md %}
Install stagehand
, which gives you templates for creating Dart apps:
> pub global activate stagehand
Note that although these instructions feature the command line, many IDEs support Dart development. Those IDEs use Stagehand behind the scenes when you create new Dart projects.
More information:
Create a command-line app:
> mkdir cli
> cd cli
> stagehand console-full
These commands create a small Dart app that has the following:
- A main Dart source file,
bin/main.dart
, that contains a top-levelmain()
function. This is the entrypoint for your app. - An additional Dart file,
lib/cli.dart
, that contains the functionality of the app and is imported by themain.dart
file. - A pubspec file,
pubspec.yaml
, that contains the app's metadata, including information about which packages the app depends on and which versions of those packages are required.
Use the pub
command to get the packages
that the app depends on:
> pub get
To run the app from the command line, use the Dart VM by running the
dart
command:
> dart bin/main.dart
Hello world: 42!
Let's customize the app you just created.
-
Edit
lib/cli.dart
to return a different result:int calculate() { return -1; }
-
Save your changes.
-
Rerun the main entrypoint of your app:
> dart bin/main.dart Hello world: -1
More information: Write command-line apps
The steps above used the Dart VM (dart
) to run the app. The Dart VM is
optimized for fast, incremental compilation to provide instant feedback
during development. Now that your small app is done, it's time to AOT compile your
Dart code to optimized native machine code.
Use the dart2aot
tool to AOT compile the program to machine code:
> dart2aot bin/main.dart bin/main.dart.aot
To run the compiled program, use the Dart runtime (dartaotruntime
):
> dartaotruntime bin/main.dart.aot
Notice how the compiled program starts instantly, completing quickly:
> time dartaotruntime bin/main.dart.aot
real 0m0.032s
Check out these resources:
- Additional tutorials and codelabs for Dart
- Dart language, libraries, and conventions
- Tools and libraries
If you get stuck, find help at Community and support.