Get Started with Server-Side Dart
What's the point?
- The Dart SDK has development tools and libraries.
- Use an IDE (such as WebStorm) or code editor to create your app.
- All Dart apps have a
- Dart supports top-level functions.
This tutorial gets you ready to begin writing Dart apps in an editor or IDE. Here you will download the Dart software and create and run a small app.
Download Dart and an IDE
Once you are ready to move beyond DartPad and create Dart apps in a real world environment, you need to download some software.
The Dart SDK contains all of the tools and libraries that you need for basic Dart development. You may also want an IDE or code editor; this tutorial uses WebStorm.
What did you get?
When you download the Dart SDK, you get a directory
that contains tools (under
bin) and libraries (under
along with supporting files.
The location of the directory (we’ll call it the <sdk-install-dir>)
depends on your platform and how you downloaded the SDK.
WebStorm has a pre-installed Dart plugin, but it requires a bit of configuration, as we’ll describe later. If you prefer to use another IDE or code editor, you might want to download and install a Dart plugin, if available. See the tools page for a list of plugins.
About Dart apps
The most minimal Dart app has the following:
- One Dart source file—a
file with the
.dartsuffix that contains Dart code
- One top-level
main()function. This is the entry point for your app.
As long as this simple Dart app can be run from the
command line (and does not rely on the browser or the Flutter SDK),
you can run it using
dart, the Dart VM tool.
$ dart main.dart
An app with any level of complexity should also include a pubspec file. The pubspec.yaml file contains the name of the app and (optionally) a description.
name: examples description: dartlang.org example code.
Pub package manager
The pub tool allows you to manage Dart packages.
Pub also includes commands for creating, developing, running, and deploying
Dart apps. Behind the scenes,
for example, uses the
dart tool to run a command-line app.
Pub uses the
pubspec.yaml file to determine
your app’s dependencies and any special setup that your app requires.
Pub assumes that the files and directories in a Dart app are
laid out in a particular way. The following diagram shows some of the
conventions used by pub. Not all of these directories are required.
- The main files for a command-line app. One of the
files must include a top-level
- Additional code to be used by your app.
- The app’s metadata, including information about which packages the app depends on and which versions of those packages are required.
You can invoke pub commands from the command line or from the WebStorm UI.
Dart command-line apps run standalone from the command line. Command-line apps are often used to provide server-side support to a web app, but they can also be scripts.
The Dart VM runs Dart code directly without intermediate compilation.
Create a command-line app
Launch WebStorm. This brings up a “Welcome to Webstorm” dialog.
If this is the first time you have run WebStorm, you will need to set the path to the SDK. You can find the instructions at Configuring Dart support.
Choose Create New Project. A dialog appears asking you to fill out a simple form.
Select Dart from the list on the left.
untitledportion of the string with
hello_world. This name is used for the app’s directory name and package name. By convention, these names are lowercase, with words separated by underscores (
Make sure that Generate sample content is checked.
Select Console app from the list.
WebStorm creates a
hello_world directory for the app
and boilerplate files for a small command-line app.
It then runs
pub get to download the packages that the app depends on.
Some of the files and directories in the hello_world app include the following:
- Support files used by
puband other Dart tools. You can ignore this.
- Contains the source files for the app.
main.dart, which is the main Dart file for this example.
- Declares which packages your app needs.
- A generated file that specifies the version numbers of the packages on which the app depends.
- Contains library code. Expand
hello_world.dart, a library file with a simple
- Tells the Dart tools where to get the packages that your
app uses. This file is created by the
pub getcommand. You can ignore this.
Double-clicking any filename displays the contents of that file in the pane to the right.
The messages pane at the bottom contains the results of calling
pub get, which fetches the packages used by the app.
When executed, the program prints
“Hello world: 42!” to the standard output stream,
print() function provided by the
The functions and objects defined in the dart:core library
are automatically available to all Dart apps.
Run a command-line app
In WebStorm, you can run the app in any of the following ways:
- Click the Run button in the upper right corner.
- Click the Run button to the left of the messages pane.
main.dartin the files pane and select Run ‘main.dart’ from the pop-up menu.
WebStorm shows the output at the bottom in a pane titled Run main.dart.
If you don’t have WebStorm, you can run the app from the command line:
$ pub run bin/main.dart
About main() and other top-level functions
Dart lets you define top-level functions,
that is, functions that are not encapsulated within a class or object.
All apps have at least one top-level function,
The app you’ve seen in this tutorial has other top-level functions.
The Hello World example calls
a top-level function defined in
A function declaration has two parts: a signature and a body.
The signature sets the function name, the data type of its return value, and the number and type of its input arguments.
The body is the code that defines the function’s behavior.
It usually appears between curly braces (
If the body is a single expression, then you
can skip the braces and use the
double milesToKM(double miles) => miles / 0.62;
milesToKM() function performs a simple arithmetic calculation
and returns the result.
This function takes a single argument. Functions can take multiple arguments, in which case the arguments are set apart by commas.
About file naming conventions
When creating an app with WebStorm,
you are asked to provide an app name.
By convention, app names
(and thus, the related files and directories) are lowercase,
with words separated by underscores (
- The WebStorm page provides more information about this tool.
- The pub pages contain more information about Dart’s package and asset manager.
Try the next tutorial, Write Command-Line Apps, which describes how to build command-line apps.