Dart is a programming language from Google, created specifically for the cross-platform Flutter framework. It combines JIT compilation for lightning-fast hot reload and AOT compilation for native performance in release builds. Dart is used not only in mobile development but also for web applications (AngularDart) and server-side solutions. Learn more about the language's capabilities in the official Dart documentation.
Key takeaways
Dart is a programming language with C-like syntax, developed by Google under the leadership of Lars Bak (creator of Eclipse). The first release was in 2011, but Dart gained real popularity in 2018 with the release of Flutter 1.0. The language is designed for building user interfaces: it has built-in support for streams, futures and reactive patterns.
Dart's philosophy is "balance of performance and productivity". The language supports two compilation strategies: JIT (Just-In-Time) for development with hot reload and AOT (Ahead-Of-Time) for release builds with native performance. The JIT compiler injects modified code into a running application in milliseconds, without requiring APK recompilation — this is Dart's key advantage over other languages in mobile development.
A key feature of Dart is sound null safety, introduced in Dart 2.12 (2021). The type system guarantees that a variable cannot be null without explicit declaration (int?). This eliminates NullPointerException at compile time. According to Google, null safety adoption reduced crash rates in Flutter applications by 30% in production environments.
Dart ecosystem includes three main areas: Flutter (mobile and desktop applications), AngularDart (web applications) and Dart Server (server-side solutions). Flutter is the dominant platform: according to Statista (2025), Flutter is used in 46% of cross-platform mobile projects, ahead of React Native (38%) and Xamarin (8%).
Dart syntax resembles Java and JavaScript but has its own unique constructs. Everything in Dart is an object — even int, double and bool inherit from Object. Typing is optional: a variable declared as var infers the type automatically, but it is recommended to explicitly specify types for readability and performance.
Dart supports int, double, String, bool, List, Set, Map, Record, Symbol and Runes. Nullable type — any type with the ? suffix (int?, String?). The ?? operator (null-aware) returns the value on the right if the left is null. Cascade notation (..) allows calling multiple methods on the same object without repeating the reference.
// ชนิดพื้นฐานและการประกาศใน Dart
void main() {
// การกำหนดชนิดอย่างชัดเจน
int age = 25;
double price = 19.99;
String name = 'Dart';
bool isValid = true;
// Null safety — ตัวแปรสามารถเป็น null ได้
int? nullableAge = null;
int result = nullableAge ?? 0; // null-aware โอเปอเรเตอร์
// คอลเลกชันที่มีเจเนอริกส์
List<String> items = ['Dart', 'Flutter', 'Mobile'];
Map<String, dynamic> config = {
'version': '3.0',
'mode': 'debug',
};
// สัญกรณ์แบบคาสเคด
final list = <String>[]
..add('one')
..add('two')
..sort();
}
// Record — ชนิดนิรนามที่ไม่สามารถเปลี่ยนแปลงได้
(String, int) user = ('Alice', 30);
print(user.$1); // 'Alice'The dynamic type disables type checking — use it only when working with JSON or dynamic data. Record is a new type in Dart 3.0 that allows grouping values without creating a separate class. The null-aware operator ?? is the standard way to work with nullable values, replacing long if-else checks.
Classes in Dart — single inheritance (extends), multiple via mixins (mixin). Dart does not support interfaces as a separate concept — any class can be implemented (implements). A mixin is a class without a constructor whose methods and fields can be added to any class using the with keyword.
// คลาส การสืบทอด และมิกซินใน Dart
abstract class Animal {
void sound(); // เมธอดนามธรรม
}
mixin Swimmer {
void swim() => print('Swimming...');
}
mixin Flyer {
void fly() => print('Flying...');
}
class Duck extends Animal with Swimmer, Flyer {
@override
void sound() => print('Quack!');
}
void main() {
final duck = Duck();
duck.sound(); // Quack!
duck.swim(); // Swimming...
duck.fly(); // Flying...
}
// คอนสตรักเตอร์แบบมีชื่อ
class Config {
final String env;
Config.development() : env = 'dev';
Config.production() : env = 'prod';
}The Duck class inherits from Animal and uses two mixins — Swimmer and Flyer. This demonstrates multiple inheritance without diamond problem issues: mixins are resolved linearly (super chain). Named constructors (Config.development) are an alternative to static factory methods for creating objects with preset parameters.
Asynchrony in Dart is implemented through Future (single value), Stream (sequence of values) and async/await. Dart uses a single-threaded model with an event loop, similar to JavaScript, but with an important difference: long computations do not block the UI because Dart code runs in an Isolate — a separate thread without shared memory.
Future represents an asynchronous operation that will complete after some time. A Future can be in pending state, completed with value, or completed with error. The .then().catchError() chain is an alternative to async/await, but await is recommended for readability.
// การโหลดข้อมูลแบบอะซิงโครนัสด้วย Future และ Stream
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
final String baseUrl;
ApiService(this.baseUrl);
// Future สำหรับคำขอเดียว
Future<Map<String, dynamic>> fetchUser(int id) async {
final response = await http.get(
Uri.parse('$baseUrl/users/$id'),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception('Failed to load user: ${response.statusCode}');
}
}
// Stream สำหรับลำดับของข้อมูล
Stream<int> countStream(int max) async* {
for (var i = 1; i <= max; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}
}
void main() async {
final api = ApiService('https://api.example.com');
// การใช้ await
final user = await api.fetchUser(1);
print('User: ${user['name']}');
// การประมวลผล Stream ด้วย await for
await for (final count in api.countStream(5)) {
print('Count: $count');
}
}The ApiService class demonstrates two types of asynchrony: Future for an HTTP request (single response) and Stream for a sequential counter (generator with async* and yield). Stream is effective for working with sensors, geolocation, Firebase Realtime Database — anywhere data arrives continuously. Isolate uses separate memory — data is transmitted via SendPort/ReceivePort.
Dart and Flutter form a single stack: Flutter is the UI framework, Dart is the UI description language. In Flutter, everything is a widget — from a button to an entire application. Widgets can be stateful (with mutable state) and stateless (immutable). Dart describes declarative UI: build() returns a widget tree that Flutter renders using Skia (or Impeller on iOS).
Flutter application in Dart consists of main() → runApp(MyApp()) → MaterialApp. MaterialApp includes routing (routes), theme and localization. Each screen is a Scaffold containing AppBar, Body (usually Column/ListView) and BottomNavigationBar. Navigation via Navigator.push/pop or named routes.
// แอปพลิเคชัน Flutter อย่างง่ายใน Dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dart Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _counter = 0;
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Dart + Flutter')),
body: Center(
child: Text('Counter: $_counter'),
),
floatingActionButton: FloatingActionButton(
onPressed: _increment,
child: Icon(Icons.add),
),
);
}
}The HomeScreen widget is a StatefulWidget with internal state _counter. Calling setState() notifies Flutter to rebuild the UI — Dart code re-runs build(), creating a new widget tree. Flutter efficiently diffs the old and new tree, updating only the changed parts of the screen. Hot Reload replaces build() without resetting the _counter state.
Dart compilation is hybrid: JIT for development and AOT for production. In debug mode, the Dart VM runs a JIT compiler that interprets bytecode with the ability to hot-swap code. In release mode, dart compile exe or flutter build creates a native AOT binary for a specific architecture (arm64, x86_64, armv7).
| Mode | Compilation | Execution speed | Binary size | Hot Reload |
|---|---|---|---|---|
| Debug | JIT (VM) | Medium | ~50 MB | Yes |
| Release | AOT (Native) | Maximum | ~15-20 MB | No |
| Web | dart2js | Depends on browser | Variable | Dev server |
Dart JIT compilation in debug mode provides hot reload in ~1 second — developers see UI changes without losing application state. AOT compilation delivers performance close to native Swift/Kotlin applications. According to Google, Flutter on Dart AOT achieves 60 FPS animations even on mid-range devices.
Dart developer tools include the Dart SDK (dart compiler, dartanalyzer, dartfmt, pub), Flutter SDK and IDE plugins. The Dart SDK is installed together with the Flutter SDK in the flutter/bin/cache/dart-sdk folder. Pub is the package manager that reads pubspec.yaml and downloads dependencies from pub.dev.
dart analyze — static analyzer that checks types, unused imports and potential errors. dart format — formatter that automatically formats code to a consistent style. dart doc — documentation generator from /// comments. Flutter DevTools — profiler with widget tree inspector, logs, memory and network views. Dart VM Service — debugging a running application via WebSocket.
# pubspec.yaml — การกำหนดค่าโปรเจกต์ Dart
name: my_flutter_app
description: A new Flutter project.
version: 1.0.0
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
provider: ^6.1.0
shared_preferences: ^2.2.0
json_annotation: ^4.8.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.4.0
json_serializable: ^6.7.0
flutter_lints: ^3.0.0The pubspec.yaml file describes Dart project dependencies. The environment:sdk field specifies the Dart version — current stable is 3.x. Dependencies are the main libraries, dev_dependencies are for testing and code generation. json_annotation + json_serializable generate JSON serialization code via build_runner. flutter_lints is a ruleset for the analyzer.
Frequently asked questions
Dart is a programming language from Google (2011), designed for cross-platform development with Flutter. Supports C-like syntax, strong typing, JIT and AOT compilation. Features sound null safety (since Dart 2.12) and asynchrony through Future and Stream. Used for mobile, web and desktop applications.
Dart compiles to native code (AOT) or JS (dart2js), JavaScript is interpreted. Dart has strong typing with sound null safety, built-in classes and mixins. Dart's asynchrony is built on Isolate (separate threads without shared memory), JS — on event loop with Web Workers. Dart AOT performance is 30-50% higher than V8 JIT in mobile scenarios.
Dart compiles AOT to native ARM code via Dart Native. In debug mode, the Dart VM uses JIT with hot reload. The Flutter Engine (C++) renders UI via Skia/Impeller. For Android — flutter build apk, for iOS — flutter build ios. Web build uses dart2js or dart2wasm. Release APK size is 15-20 MB (without AAB splitting).
Dart supports primitives: int, double, String, bool. Collections: List, Set, Map, Record. Special: Symbol, Runes, Null. Everything is an object, including primitives. Null safety: int? allows null, int is never null. Operators ??, ??= for safe null handling. Generics: List<String>, Map<String, dynamic>.
Flutter SDK includes the Dart SDK, Flutter Engine and widget library. IDE — Android Studio or VS Code with Flutter plugin. DevTools — profiler with widget inspector. Pub.dev — package manager with 45,000+ libraries. The dart analyze and dart format tools ensure code quality. Build_runner for code generation of JSON models.
Summary
เราจะพัฒนาแอปพลิเคชันบนมือถือแบบครบวงจร
IT Sectr สร้างแอปพลิเคชัน iOS และ Android สำหรับสตาร์ทอัพและธุรกิจตั้งแต่ปี 2017 เราจะให้คำแนะนำและเสนอวิธีแก้ปัญหาที่ดีที่สุดแก่คุณ