What Is Dart? — A Complete Introduction to the Language
In this tutorial, you will learn about What Is Dart?. We cover key concepts, practical examples, and best practices to help you master this topic.
Dart is a client-optimized, garbage-collected programming language developed by Google, designed for building fast, production-quality applications on any platform using ahead-of-time compilation and sound null safety.
What You Will Learn
- What Dart is and why Google created it
- Key features: sound null safety, type inference, async/await
- The difference between JIT and AOT compilation
- How Dart powers the Flutter framework
- The Dart ecosystem: packages, tools, and community
Why It Matters
Dart is the language behind Flutter, the most popular cross-platform framework for building mobile, web, and desktop applications from a single codebase. Companies like Google, BMW, Alibaba, and ByteDance use Dart in production. Learning Dart opens the door to Flutter development, which has over 500,000 apps published on the Play Store. Dart's unique compilation model combines fast development iteration (JIT with hot reload) with native performance (AOT compilation), a combination that few other languages offer.
Real-World Use
The DodaTech companion mobile app is built with Flutter and Dart. The app uses Dart's async/await for API calls, sound null safety to prevent crashes, and AOT-compiled native binaries for smooth scrolling and animations. The same Dart code compiles for Android, iOS, and web with no changes.
Learning Path
flowchart LR A[Introduction to Programming] --> B[What Is Dart?\nYou are here] B --> C[Dart Installation] style B fill:#f90,color:#fff
What Exactly Is Dart?
Dart is a general-purpose programming language created by Google in 2011. It was designed to replace JavaScript as the language of the web, but it evolved into a client-optimized language for building user interfaces. Dart compiles to native ARM and x64 machine code for mobile and desktop, and to JavaScript for web browsers.
The language syntax is similar to Java, C#, and JavaScript, making it easy to learn if you have experience with any of those. Dart is object-oriented and supports classes, interfaces, mixins, generics, and optional typing with type inference.
Dart's Two Compilation Modes
Dart has a unique dual compilation strategy that gives you the best of both development speed and runtime performance:
JIT (Just-in-Time) Compilation is used during development. The Dart VM compiles code on the fly as the application runs. JIT enables Flutter's hot reload feature, where changing code takes effect in under a second without restarting the app. The compilation overhead is acceptable during development because developer productivity matters more than absolute speed.
AOT (Ahead-of-Time) Compilation is used for production releases. The Dart compiler translates your Dart code directly into native machine code before the app runs. AOT-compiled code starts faster, runs with consistent performance, and does not require a VM at runtime. When you run flutter build apk --release, Flutter compiles your Dart code AOT into native ARM or x64 instructions.
void main() {
print('Dart compiles to native machine code for production');
print('During development, the JIT VM enables hot reload');
}
Output:
Dart compiles to native machine code for production
During development, the JIT VM enables hot reload
Sound Null Safety
Dart's most important feature is sound null safety, introduced in Dart 2.12. The type system distinguishes between nullable types (with ?) and non-nullable types (without ?). This distinction is enforced at compile time, eliminating null pointer exceptions at runtime.
void main() {
// Non-nullable variable - cannot be null
String name = 'Alice';
// name = null; // COMPILE ERROR: null can't be assigned to String
// Nullable variable - can be null
String? maybeName = null;
maybeName = 'Bob'; // OK
// Accessing a nullable value requires handling null
String greeting = 'Hello, ${maybeName ?? 'Guest'}!';
print(greeting);
}
Output:
Hello, Bob!
The ? suffix on String? declares a nullable string. The ?? operator provides a default value when the expression is null. Dart's flow analysis is smart enough to promote a nullable type to non-nullable after a null check.
Type Inference
Dart supports both explicit type annotations and type inference with the var keyword. The compiler infers the type from the assigned value:
void main() {
var count = 42; // inferred as int
var message = 'Hello'; // inferred as String
var items = [1, 2, 3]; // inferred as List<int>
var pair = ('key', 100); // inferred as Record
// Use explicit types when inference is ambiguous
Object something = 'This could be anything';
}
Use var when the type is obvious from the right-hand side. Use explicit types when the type is not obvious or when you want to constrain the variable to a supertype.
The Dart Standard Library
Dart ships with a comprehensive standard library that covers most common programming needs:
import 'dart:math';
import 'dart:convert';
import 'dart:io';
void main() {
// Math utilities
print(sqrt(16)); // 4.0
// JSON encoding and decoding
var jsonString = jsonEncode({'name': 'Alice', 'age': 30});
print(jsonString);
// File I/O is available on server-side Dart
// For Flutter, use the dart:io subset or path_provider plugin
}
Output:
4.0
{"name":"Alice","age":30}
The standard library includes packages for math, JSON, HTTP, file I/O, collections, cryptography, and more. For platform-specific APIs (camera, sensors, storage), use Flutter plugins from pub.dev.
Dart and Flutter
Dart is the programming language; Flutter is the UI framework. Understanding the difference is important:
- Dart provides the language features: types, functions, classes, async/await, streams.
- Flutter provides the UI widgets: Text, Row, Column, Container, MaterialApp.
When you write a Flutter app, you use Dart to express the widget tree and manage state. Flutter does not use a separate markup language like XML or HTML. Everything is Dart code:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Hello Dart')),
body: Center(
child: Text('Learning Dart with Flutter'),
),
),
),
);
}
Every widget is a Dart class. The runApp function mounts the widget tree. Flutter handles rendering, layout, and event handling. You write Dart to describe what the UI should look like.
The Dart Ecosystem
The Dart ecosystem centers around pub.dev, the official package repository with over 40,000 packages. Popular packages include:
- http: Making HTTP requests
- json_serializable: Automatic JSON serialization with Code Generation
- provider: State management for Flutter
- sqflite: SQLite database for mobile
- Firebase_core: Firebase integration
Packages are managed with pubspec.yaml:
name: my_app
dependencies:
http: ^1.2.0
json_annotation: ^4.8.0
dev_dependencies:
build_runner: ^2.4.0
json_serializable: ^6.7.0
Run dart pub get or flutter pub get to download dependencies.
Tools
The Dart SDK includes everything you need to write and run Dart code:
# Compile and run a Dart file
dart run main.dart
# Format code
dart format main.dart
# Analyze for issues
dart analyze
# Run tests
dart test
# Compile to native executable
dart compile exe main.dart -o myapp
The Dart Analyzer catches type errors, unused imports, and style violations. Running dart analyze is part of every build pipeline.
Common Mistakes
Confusing Dart with JavaScript: Syntax may look similar, but Dart is class-based, statically typed, and compiles natively. Patterns valid in JavaScript (type coercion, hoisting) do not apply.
Not using sound null safety: All new Dart projects enable null safety by default. If you see warnings about null safety, check your SDK constraint in pubspec.yaml and migrate existing code with
dart migrate.Blocking the event loop with synchronous I/O: Dart has a single-threaded event loop. Use
async/awaitfor all I/O operations. CallingFile.readAsStringSync()blocks the entire application.Overusing
dynamictype:dynamicdisables Type Checking entirely. PreferObject?or a specific type, and use type promotion withischecks instead.Forgetting to dispose resources: Stream subscriptions, timers, and file handles must be disposed. In Flutter, override
dispose()in StatefulWidget. In Dart, use theusingpattern or close resources explicitly.
Practice Questions
- What is the difference between JIT and AOT compilation in Dart?
- How does sound null safety prevent null pointer exceptions at compile time?
- What is the relationship between Dart and Flutter?
- Why does Dart use a single-threaded event loop instead of threads?
- Challenge: Write a Dart program that reads a JSON file, parses it using
dart:convert, and prints the values in a formatted table. Use sound null safety and handle file-not-found errors gracefully.
Mini Project
Create a command-line Dart application that:
- Defines a data model for a Book (title, author, year, ISBN)
- Implements a function to serialize a list of books to JSON
- Implements a function to deserialize JSON back to books
- Reads and writes books to a file on disk
- Uses sound null safety and proper error handling
FAQ
What is Next
Now that you understand what Dart is, get it installed on your system. Proceed to Dart Installation Guide for platform-specific setup instructions. Then continue with Dart Variables and Data Types to learn the fundamentals.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro