Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 61 ยท Getting Started

Installing the Dart SDK and Tooling

Install the Dart SDK, create a project with dart create, run it, add packages with dart pub, and use the analyzer, formatter, DartPad and VS Code.

You can learn a lot of Dart in a browser, but real projects live on your own machine. The good news is that Dart's tooling is unusually compact: one command, dart, creates projects, runs them, installs packages, checks your code, formats it, tests it and compiles it. There is no separate build tool to learn.

This lesson walks through installing the SDK, creating your first project, and the commands you will use every day. All commands here are shown as read-only text, because they run on your computer, not on this site. A small TypeScript stand-in at the end shows what the analyzer does.

Getting the SDK

The Dart SDK contains the dart command, the core libraries, the analyzer and the compilers. There are two common ways to get it:

  • Install Dart on its own. The Dart website lists current instructions for each system. On macOS you can use Homebrew, on Windows a package manager such as Chocolatey or winget, and on Linux the official package repository or a downloaded archive.
  • Install Flutter. The Flutter SDK bundles its own copy of Dart. If your goal is Flutter, install Flutter and you already have the dart command.

Package names and repository URLs change over time, so follow the current instructions on dart.dev rather than copying a command from an old article. When the install finishes, open a new terminal and check it:

TEXT
dart --version

You should see a line that names the Dart SDK version. This tutorial targets Dart 3 or later.

If dart is "not found" after installing, the SDK's bin folder is not on your PATH. Close and reopen the terminal first; if that does not help, add the folder to PATH as the installer's instructions describe.

Creating a project

The dart create command generates a project from a template. The most useful one for learning is the console application:

TEXT
dart create my_app
cd my_app
dart run

dart create uses the console template by default. Other templates include package (a reusable library), server-shelf (a web server) and web (a browser app). You can pick one with -t, for example dart create -t package my_library.

Here is what the generated folder looks like:

flowchart TD
    R["my_app/"] --> P["pubspec.yaml<br/>name, SDK version, dependencies"]
    R --> A["analysis_options.yaml<br/>lint rules"]
    R --> B["bin/<br/>my_app.dart (entry point)"]
    R --> L["lib/<br/>shared code"]
    R --> T["test/<br/>tests"]

The standard layout of a Dart project. Every package follows the same structure, which is why tools work the same everywhere.

  • pubspec.yaml describes the package: its name, the Dart versions it supports and the packages it depends on.
  • bin/my_app.dart holds the main() function that starts the program.
  • lib/ is for code that other files (or other packages) import.
  • test/ contains tests, which you will write in a later lesson.
  • analysis_options.yaml configures the analyzer and lint rules.

Open bin/my_app.dart and you will see a tiny program like this one:

DART
import 'package:my_app/my_app.dart' as my_app;

void main(List<String> arguments) {
  print('Hello world: ${my_app.calculate()}!');
}

The exact text depends on your SDK version. What matters is that running dart run prints a greeting. You have a working Dart project.

The commands you will use every day

Everything goes through dart. These are the ones worth memorizing:

Command What it does
dart run Runs the project's bin/ program (or dart run path/to/file.dart for one file)
dart pub add http Adds a package to pubspec.yaml and downloads it
dart pub get Downloads every dependency listed in pubspec.yaml
dart analyze Checks the whole project for errors, warnings and lint messages
dart format . Reformats all files in the standard Dart style
dart fix --apply Automatically applies the fixes the analyzer suggests
dart test Runs the tests in test/
dart compile exe bin/my_app.dart Builds a native executable
dart compile js file.dart Compiles to JavaScript

You can also run a single file without any project at all. Save a file named hello.dart and run dart run hello.dart, or simply dart hello.dart.

Packages and pub.dev

Dart's package manager is called pub, and the public package repository is pub.dev. A command like dart pub add collection edits pubspec.yaml and writes a pubspec.lock file that pins the exact versions you got. Commit the lock file for applications; libraries usually do not. The later lesson on libraries and packages goes deeper.

The analyzer and the formatter

Two tools do a surprising share of the work.

The analyzer reads your code without running it and reports problems: type errors, unused variables, missing awaits and style issues. Your editor shows the same messages as you type, and dart analyze prints them for the whole project. A problem comes in three levels: errors (the code will not compile), warnings (probably a bug) and info (a suggestion or lint). Lint rules you want to enforce are listed in analysis_options.yaml:

YAML
include: package:lints/recommended.yaml

linter:
  rules:
    - prefer_final_locals
    - avoid_print

The formatter, dart format, rewrites your code into one agreed layout: two-space indentation, a standard line length and consistent line breaks. It is not configurable on purpose, which ends style arguments. Run it before every commit, or let your editor do it when you save.

Editors and DartPad

You can write Dart in any text editor, but two choices stand out:

  • Visual Studio Code with the official Dart extension (and Flutter if you need it). You get completion, inline errors, quick fixes, refactoring, a debugger and test running.
  • IntelliJ IDEA or Android Studio with the Dart plugin, which offers similar features in a full IDE.

If you only want to try something quickly, DartPad at dartpad.dev runs Dart in your browser with no installation. It is perfect for experiments and for sharing small snippets.

A stand-in analyzer

To get a feel for what the analyzer does, here is a toy version written in TypeScript. It reads some Dart source as plain text and reports a lint message for each print( call, like a project that enabled the avoid_print rule. The real analyzer parses the code properly; this stand-in only scans lines.

TypeScript A toy lint check (TypeScript stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

The output is two info lines and 2 issues found.. Real dart analyze output has the same spirit: a severity, a location, a message and the name of the rule, so you can look up or silence it.

A project layout as data

Another small stand-in prints the folder structure from the diagram above. It is only a way to practice reading the layout.

TypeScript Printing the project layout (TypeScript stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command creates a new console project named my_app?

    dart create my_app generates a project from the default console template.
  2. 2. Where does the main() function of a console project normally live?

    Entry points live in the bin folder. The lib folder is for code you import from other files and packages.
  3. 3. What is the purpose of dart format?

    The formatter only changes layout, such as indentation and line breaks. Finding errors is the analyzer's job.
  4. 4. What does dart pub add http change?

    pub add edits the dependencies in pubspec.yaml and fetches the package, updating pubspec.lock for this project only.

Summary

  • The Dart SDK provides one dart command for creating, running, analyzing, formatting, testing and compiling.
  • dart create makes a project with pubspec.yaml, bin/, lib/ and test/.
  • dart pub add installs packages from pub.dev; dart pub get restores them.
  • The analyzer finds problems without running the code, and dart format enforces one standard style.
  • DartPad runs Dart in a browser; VS Code and IntelliJ-based editors have first-class Dart support.

Next, write and run real code in Your First Dart Program.