Thuta Learning
Dart
BasicProgrammingBeginner

Get Started

DartLesson 325 min

What you'll walk away with

  • Choose between DartPad in the browser and a local SDK for the task at hand
  • Check an installation with dart --version and recognize a PATH problem
  • Run a single file with dart run hello.dart and a project with dart run
  • Create a project with dart create and say what pubspec.yaml, bin/ and lib/ are for

Explain it simply

Setup, simply

Before Dart can run your code, something on your computer has to read it. You have two easy options. DartPad is a web page where you type Dart and press Run, with nothing to install. The Dart SDK is a download that puts one command, dart, on your computer; it runs files, checks them, formats them and creates new projects.

Start with DartPad if you only want to try an example in the next minute, and install the SDK as soon as you want to keep files, build a project or follow a Flutter tutorial. Both run exactly the same Dart code.

Problem → Why → Solution

The problem: the code is right but nothing runs

You copy a program from a lesson exactly, save it as hello.dart and type dart run hello.dart, and the terminal says it does not know dart. In Bash that is bash: dart: command not found; in PowerShell it is The term 'dart' is not recognized as the name of a cmdlet, function, script file, or operable program. You fix that, and the next error says the file cannot be found.

Read quickly, both messages look as if your Dart code is wrong, but neither is about the code. They are about where things are on your computer.

terminal
$ dart run hello.dart
bash: dart: command not found

# later, after installing the SDK, but in the wrong folder:
$ dart run hello.dart
Could not find file `hello.dart`

Why these errors happen

The first message means the shell searched every folder listed in your PATH and found no program named dart: the SDK is not installed, its bin folder is not on PATH, or the terminal was opened before the PATH changed. The second means the shell found dart, but the file is not in the current folder.

Core concept: find the tool, then find the file

First choose the option that fits you. The table below says what each one is best for. Every example in this course is a single file with a main(), so all four options work.

OptionWhat you needBest for
DartPadJust a browser (dartpad.dev)Trying examples quickly
Dart SDK + terminalDownload the SDK and use a terminalSaving files, building projects, scripts and CI
Editor + Dart pluginThe Dart extension for VS Code, or the Dart plugin for IntelliJ and Android StudioAutocomplete and seeing errors while you type
Flutter SDKInstalling Flutter also gives you a dart commandWhen your goal is Flutter apps

Now the command-line route, step by step. Check the result of each step before moving on, and when something goes wrong you will know at once which step caused it.

1. Install the SDK

Follow the install page on dart.dev for your operating system. Package managers such as Homebrew on macOS, Chocolatey on Windows and apt on Linux are common, and a zip download whose bin folder you add to PATH also works.

2. Check it in a new terminal

Open a new terminal and run dart --version. A line starting with Dart SDK version: followed by the version number should appear. Terminals opened before the PATH change keep the old PATH.

3. Run a single file

Save hello.dart, cd into the folder that holds it and type dart run hello.dart. The file name must match exactly.

4. Create a project when one file is not enough

Use dart create hello_app to make a project folder, cd hello_app, then type dart run. Inside a project, dart run needs no file name; it finds the entry point under bin/.

A single file needs nothing but a main(). A project adds pubspec.yaml (the package name, the SDK constraint and the dependencies), bin/ for entry points, lib/ for reusable code, test/ for tests and analysis_options.yaml for lint rules.

Mental model: find the tool, then the file
  1. Terminal finds dart on PATH
  2. dart finds your file or project
  3. The VM runs main(), output prints

Almost every first-day error is one of these two lookups failing, so ask which one failed before touching the code.

General pattern: the command-line workflow

bash
dart --version
dart run file_name.dart
dart create project_name
cd project_name
dart run

The first line proves the SDK is on PATH. dart run file_name.dart runs one file. dart create makes a project folder, and inside it dart run starts from the entry point under bin/. You can pick any file and project names you like.

A first program to prove your setup

Save this program as hello.dart and run it with the command from the comment. In DartPad, paste it and press Run. If two lines appear, both the SDK and your file are readable. The second line contains a calculation, which makes it clearer that the program really ran.

hello.dart
// hello.dart: save this file, then run: dart run hello.dart
void main() {
  print('Dart setup is ready.');
  print('2 + 3 = ${2 + 3}');
}

Code breakdown

  1. A comment with the command// hello.dart: save this file, then run: dart run hello.dart

    A line starting with // is a comment, and Dart skips it. It reminds you of the file name and the command to run.

  2. The entry pointvoid main() {

    dart run starts from this function. A file without main() gives it nothing to run, so Dart refuses.

  3. Printing textprint('Dart setup is ready.');

    print() writes the text to the terminal and moves to a new line. If this line appears, the SDK works.

  4. A calculation inside a stringprint('2 + 3 = ${2 + 3}');

    The 2 + 3 inside ${ } is worked out first and the result, 5, is placed into the text, so the output is '2 + 3 = 5'.

You should see
Dart setup is ready.
2 + 3 = 5
From the terminal to the output
  1. You type dart run hello.dart
  2. The shell finds dart on PATH
  3. dart reads and checks hello.dart
  4. main() runs and print writes output

Every step succeeds

Two lines appear and the command returns to the prompt

One step fails

The message names the step: command not found, file not found, or a code error

What a Dart project contains

dart create hello_app builds a folder shaped like the one below. pubspec.yaml is the project's identity card: its name, its version, which SDK versions it works with and which packages it needs.

text
Layout created by dart create hello_app
---------------------------------------
hello_app/
  pubspec.yaml              name, SDK range, dependencies
  analysis_options.yaml     lint rules used by dart analyze
  bin/hello_app.dart        entry point: dart run starts here
  lib/hello_app.dart        reusable code that other files import
  test/hello_app_test.dart  tests run by dart test
pubspec.yaml
name: hello_app
description: A sample command-line application.
version: 1.0.0

environment:
  sdk: ^3.0.0

sdk: ^3.0.0 means Dart 3.0.0 and above, up to but not including Dart 4. dart create writes a number that matches the SDK you are using, so the number in your own pubspec.yaml can differ from the one shown here.

The dart commands you will use every day

CommandWhat it does
dart runRuns a file, or a project's entry point
dart analyzeFinds errors and warnings without running anything
dart format .Rewrites code layout to the standard Dart style
dart testRuns the tests in a project
dart pub getDownloads the packages listed in pubspec.yaml
dart compile exeTurns a program into a self-contained executable

Troubleshooting the first-day errors

MessageLikely causeFix
bash: dart: command not foundThe SDK is missing or its bin folder is not on PATHInstall it, add it to PATH, open a new terminal
Could not find file `hello.dart`Wrong folder or wrong file namecd into the folder and check it with ls or dir
Found no `pubspec.yaml` filedart run with no file name outside a project folderName the file (dart run hello.dart) or cd into the project

In real projects

How teams handle setup in practice

On a team project nobody keeps a random SDK version on their machine. The sdk constraint in pubspec.yaml says which Dart versions the project accepts, and CI servers install a matching version with an action such as dart-lang/setup-dart and then run dart pub get, dart analyze and dart test in order. Every developer and the server therefore build with the same tools.

Inside an editor, the Dart extension for VS Code and the Dart plugin for IntelliJ run the analyzer in the background, so you see the red underlines while typing without running dart analyze yourself. Understanding the commands still matters: when the editor cannot run something, you can type the same command in a terminal and tell whether the problem is the tooling or your code.

Common mistakes

Common setup mistakes

1. Using an old terminal after changing PATH

command not found keeps appearing even after the install. A terminal that was already open keeps the old PATH. Close it and open a new one; for the terminal built into VS Code, restart VS Code as well.

2. Running from the wrong folder

A terminal usually starts in your home folder, not in the folder that holds your file. When Could not find file appears, cd into the right folder and list it with ls (dir on Windows) so you can see the exact file name.

3. Saving hello.dart.txt by accident on Windows

Windows often hides file extensions, so saving from Notepad can produce hello.dart.txt. The code inside is fine, yet dart run hello.dart cannot find the file. Turn extensions on and rename the file to hello.dart.

Mini challenge

Mini challenge: a setup checker

Write a program that reads the major version of your Dart SDK and reports whether it meets this course's Dart 3 requirement and which operating system it runs on.

  • Import dart:io and use Platform.version
  • A majorVersion() function returns the first number in Platform.version as an int
  • In main(), print the major version and the result of major >= 3 (true or false)
  • Print Platform.operatingSystem as well
check.dart
import 'dart:io';

int majorVersion() {
  // TODO: Platform.version starts with text like '3.4.0 (stable) ...'
  // TODO: keep the part before the first space, then the part before the first dot
  // TODO: convert it with int.parse and return it
}

void main() {
  // TODO: print the major version
  // TODO: print whether it is 3 or higher
  // TODO: print Platform.operatingSystem
}

Expected: On a Dart 3 SDK the first line is Dart major version: 3 and the second is Dart 3 or newer: true. The third line is windows, linux or macos, depending on your machine.

Show hint

Platform.version.split(' ').first gives text such as '3.13.7'. Split that again with .split('.').first and pass the result to int.parse.

Show solution
check.dart
import 'dart:io';

int majorVersion() {
  final firstWord = Platform.version.split(' ').first;
  return int.parse(firstWord.split('.').first);
}

void main() {
  final major = majorVersion();
  print('Dart major version: $major');
  print('Dart 3 or newer: ${major >= 3}');
  print('Operating system: ${Platform.operatingSystem}');
}

Quick quiz 1 / 3

After installing the SDK, dart --version works in a new terminal but not in the terminal that was already open. Why?

Quick quiz 2 / 3

Which command creates a new Dart project folder called hello_app?

Quick quiz 3 / 3

In the top folder of a project made by dart create hello_app, which file does dart run start from?

What to remember

  • DartPad is for trying code with no install, the Dart SDK is for files and projects, and the Flutter SDK includes a dart command too
  • Run dart --version in a new terminal to check the install; a PATH change needs a new terminal
  • dart run hello.dart runs one file, and inside a project dart run starts the entry point under bin/
  • Most first-day errors mean the tool or the file was not found, not that the code is wrong

Dart 3.13 changelog — Dart

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Get Started | Thuta Learning