Introduction to Dart
Learn the history, setup, and first steps with the Dart programming language.
History of Dart
Dart is a client-optimized, general-purpose programming language developed by Google. It was first announced at the GOTO conference in Denmark in October 2011.
- 2011: Dart announced by Google as a scalable language for web apps
- 2013: Dart SDK 1.0 released with VM and dart2js compiler
- 2015: Dart 1.12 with sound null safety prototyping begins
- 2018: Dart 2.0 ships with sound null safety, required for Flutter
- 2020: Dart 2.12 enforces null safety by default
- 2023: Dart 3.0 introduces records, patterns, and class modifiers
- 2024–2026: Continued evolution with macro support and improved AOT compilation
Today, Dart is the language behind Flutter, Google's cross-platform UI toolkit used by millions of developers worldwide.
Verifying Your Installation
After installing the SDK, verify everything works by running these commands in your terminal:
# Check Dart version
dart --version
# Create a new project
dart create my_first_project
# Run the project
cd my_first_project
dart run
The dart create command scaffolds a complete project structure with a pubspec.yaml (dependency manifest), a lib/ directory for your source code, and a test/ directory for unit tests. This is the standard layout used by the entire Dart and Flutter ecosystem.
flutter.dev/get-started.
Setting Up the Dart SDK
You can install Dart either standalone or as part of the Flutter SDK. Here's how to get started:
Option A: Standalone Dart SDK
# Using Homebrew on macOS
brew tap dart-lang/dart
brew install dart
# Verify installation
dart --version
# Using Chocolatey
choco install dart-sdk
# Or download the installer from dart.dev
# https://dart.dev/get-dart
Option B: Via Flutter SDK (Recommended)
If you're planning to build Flutter apps, install the full Flutter SDK which includes Dart:
# Clone the Flutter SDK
git clone https://github.com/flutter/flutter.git -b stable
# Add to PATH
export PATH="$PATH:`pwd`/flutter/bin"
# Verify Flutter + Dart
flutter doctor
dart --version
stable channel for production projects. The beta and dev channels may have breaking changes.
main() function is the entry point of every Dart application. Without it, the Dart VM has no idea where to start executing your code. Think of it as the front door to your program — the Dart runtime knocks, and your main() function answers.
When you run a Dart program, the Dart VM (Virtual Machine) or the Dart native compiler looks for the main() function first. If it can't find one, you'll get a compilation error. This is different from languages like Python, where code can execute at the module level.
Dart's Compilation Pipeline
Understanding how Dart processes your code is crucial for writing performant applications:
| Phase | Description | Tool |
|---|---|---|
| Lexical Analysis | Converts source code into tokens | Scanner |
| Parsing | Builds an Abstract Syntax Tree (AST) | Parser |
| Resolution | Resolves types and scopes | Compiler |
| Code Generation | Produces bytecode or machine code | VM / AOT |
Dart supports two compilation modes: VM (Just-In-Time) for development with hot reload, and AOT (Ahead-Of-Time) for production builds that compile to native machine code. This dual-mode capability is what makes Flutter development so fast and productive.
Your First Dart Program
Create a new file called main.dart and add the following:
void main() {
print('Hello, Dart!');
print('Welcome to dftacademy');
}
Run the program:
dart run main.dart
# Output:
# Hello, Dart!
# Welcome to dftacademy
The main() Function
Every Dart program starts with the main() function. It serves as the entry point:
void main()— basic entry point with no argumentsvoid main(List<String> args)— entry point that accepts command-line arguments
void main(List<String> args) {
if (args.isNotEmpty) {
print('Hello, ${args[0]}!');
} else {
print('Hello, World!');
}
}
dart run main.dart DartDev
# Output: Hello, DartDev!
Real-World Example: Price Calculator
Let's build a practical command-line script that calculates item prices with tax and discounts:
void main() {
// Product data
var itemName = 'Wireless Keyboard';
var price = 49.99;
var quantity = 3;
var taxRate = 0.08; // 8% tax
var discountPercent = 10; // 10% bulk discount
// Calculations
var subtotal = price * quantity;
var discountAmount = subtotal * (discountPercent / 100);
var afterDiscount = subtotal - discountAmount;
var taxAmount = afterDiscount * taxRate;
var total = afterDiscount + taxAmount;
// Output receipt
print('╔══════════════════════════════╗');
print('║ ORDER RECEIPT ║');
print('╠══════════════════════════════╣');
print('║ Item: ${itemName.padRight(20)}║');
print('║ Price: \$${price.toStringAsFixed(2)} × $quantity ║');
print('║ Subtotal: \$${subtotal.toStringAsFixed(2).padLeft(8)} ║');
print('║ Discount (${discountPercent}%): -\$${discountAmount.toStringAsFixed(2).padLeft(6)} ║');
print('║ Tax (${(taxRate * 100).toInt()}%): +\$${taxAmount.toStringAsFixed(2).padLeft(7)} ║');
print('║──────────────────────────────║');
print('║ TOTAL: \$${total.toStringAsFixed(2).padLeft(8)} ║');
print('╚══════════════════════════════╝');
}
dart run price_calculator.dart
# ╔══════════════════════════════╗
# ║ ORDER RECEIPT ║
# ╠══════════════════════════════╣
# ║ Item: Wireless Keyboard ║
# ║ Price: $49.99 × 3 ║
# ║ Subtotal: $149.97 ║
# ║ Discount (10%): -$15.00 ║
# ║ Tax (8%): +$10.80 ║
# ║──────────────────────────────║
# ║ TOTAL: $145.77 ║
# ╚══════════════════════════════╝
Summary
- Dart is a modern, strongly-typed language created by Google
- The
main()function is the entry point for every Dart program print()outputs text to the console- Use
varfor type inference and explicit types for clarity - Dart supports string interpolation with
${}syntax