Introduction to Dart
What is Dart?
Dart is a client-optimized programming language developed by Google for building fast apps on any platform. It's designed for creating high-performance applications with a focus on:
- UI development: Build beautiful, responsive user interfaces
- Cross-platform: Write once, run on mobile, web, and desktop
- Productivity: Hot reload, rich standard library, and excellent tooling
- Performance: Ahead-of-time (AOT) compilation for production, just-in-time (JIT) for development
History and Evolution
- 2011: Dart was unveiled by Google at the GOTO conference
- 2013: Dart 1.0 released
- 2018: Dart 2.0 introduced with strong type system
- 2020: Dart 2.12 introduced sound null safety
- 2023+: Continuous improvements with Flutter's growth
Key Features
1. Object-Oriented
Dart is a pure object-oriented language where everything is an object, including numbers, functions, and even null.
2. Strong Type System
Dart uses static type checking with type inference, catching errors at compile time while keeping code concise.
3. Null Safety
Sound null safety prevents null reference errors, one of the most common programming mistakes.
4. Asynchronous Programming
Built-in support for async/await makes handling asynchronous operations elegant and readable.
5. Rich Standard Library
Comprehensive collection of libraries for common tasks like collections, I/O, math, and more.
6. Excellent Tooling
- DartPad: Online editor for quick experiments
- Dart DevTools: Debugging and performance profiling
- Package Manager: pub.dev for sharing and using packages
Dart and Flutter
While Dart can be used for various applications, it gained massive popularity through Flutter, Google's UI toolkit for building natively compiled applications. Flutter uses Dart exclusively, making Dart knowledge essential for Flutter developers.
Installation
Install Dart SDK
Windows:
choco install dart-sdkmacOS:
brew tap dart-lang/dart
brew install dartLinux:
sudo apt-get update
sudo apt-get install apt-transport-https
wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg
echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list
sudo apt-get update
sudo apt-get install dartVerify Installation
dart --versionYour First Dart Program
Create a file named hello.dart:
void main() {
print('Hello, Dart!');
}Run it:
dart run hello.dartOutput:
Hello, Dart!Development Environment
Recommended IDEs
Visual Studio Code
- Install the "Dart" extension
- Lightweight and fast
Android Studio / IntelliJ IDEA
- Install the "Dart" plugin
- Full-featured IDE with excellent debugging
DartPad (Online)
- Visit dartpad.dev
- No installation required
- Great for learning and sharing code
Dart Execution Modes
1. JIT (Just-In-Time) Compilation
- Used during development
- Enables hot reload
- Faster development cycle
2. AOT (Ahead-Of-Time) Compilation
- Used for production
- Compiles to native machine code
- Better performance and faster startup
Basic Program Structure
// Import statements
import 'dart:math';
// Top-level variables
const String appName = 'My App';
// Top-level functions
void greet(String name) {
print('Hello, $name!');
}
// Main entry point
void main() {
greet('World');
print('Random number: ${Random().nextInt(100)}');
}Comments
// Single-line comment
/*
Multi-line comment
Can span multiple lines
*/
/// Documentation comment
/// Used for generating API documentation
void documentedFunction() {
// Function body
}Next Steps
Now that you understand what Dart is and have it installed, let's dive into the fundamentals:
Happy coding! 🎯