Installation, Tools and First Program

2.1 Installing Dart

Editors: VS Code (the Dart and Flutter extensions), IntelliJ IDEA / Android Studio (the Dart plugin). The Dart Analysis Server powers IDE features (LSP).

DartPad (dartpad.dev) lets you run Dart and Flutter in the browser with no installation.

# macOS
brew tap dart-lang/dart && brew install dart
# Windows
choco install dart-sdk        # or: winget install Dart.Dart-SDK
# Linux (Debian/Ubuntu): add Google's apt repository, then:
sudo apt-get install dart
# Or install Flutter (bundles Dart):  flutter --version  /  dart --version
dart --version

2.2 The dart Command

dart create -t console my_app Create a project (templates: console, package, server-shelf, web, cli) dart run / dart run bin/main.dart dart compile exe bin/main.dart Self-contained native executable (AOT)

dart compile aot-snapshot / jit-snapshot / kernel Other compilation formats dart compile js / dart compile wasm Compile to JavaScript / WebAssembly

dart pub get / add / upgrade / outdated / publish dart analyze dart format. dart fix --apply

Apply automated fixes (migrations, lints) Run tests (with package:test) Generate API documentation dart devtools

2.3 Hello, World

// bin/main.dart
void main() {
  print('Hello, World!');

  var name = 'Dart';
  print('Hello, $name! Version ${3 + 0}.x');   // string interpolation
}
dart create -t console hello_dart
cd hello_dart
dart run

2.4 Command-Line Arguments and Input

import 'dart:io';

void main(List<String> arguments) {
  print('Arguments: $arguments');
  stdout.write('What is your name? ');
  final name = stdin.readLineSync() ?? 'stranger';   // returns String? (null at EOF)
  print('Nice to meet you, $name!');
}

2.5 Project Structure

my_app/
├── bin/main.dart          # executable entry points
├── lib/                   # library code (public API in lib/, private code in lib/src/)
│   ├── my_app.dart
│   └── src/helpers.dart
├── test/my_app_test.dart
├── pubspec.yaml           # package metadata and dependencies
├── pubspec.lock           # resolved versions (commit for applications)
├── analysis_options.yaml  # lints and analyzer configuration
└── README.md / CHANGELOG.md

2.6 Comments

// Single-line comment
/* Multi-line comment */
/// Documentation comment (used by dart doc). Supports [references] and Markdown.