Dart
ภาษาโปรแกรมมิ่งที่สร้างโดย Google เป็นภาษาหลักที่ใช้เขียน Flutter
จุดเด่น
- รองรับทั้ง AOT (Ahead of Time) และ JIT (Just in Time) compilation
- Null safety — ช่วยลด bug เรื่อง null
- Async/await — จัดการ asynchronous ได้สะดวก
- Strong typing แต่มี type inference
- ทุกค่าเป็น Object (เหมือน Java)
ระบบ Type
Dart เป็นภาษา type-safe — ทุกค่าต้องมี type กำกับ และค่าหนึ่งมี หลาย type ได้:
'Hello World!' → เป็นทั้ง String และ Object
29 → เป็นทั้ง int, num และ Object
MaterialApp() → เป็นทั้ง MaterialApp, Widget และ ObjectCore Types
| Type | คืออะไร | ตัวอย่าง |
|---|---|---|
int | จำนวนเต็ม | 29, -15 |
double | ทศนิยม | 3.91, -12.81 |
num | เต็มหรือทศนิยม | 15, 15.01 |
String | ข้อความ | 'Hello World' |
bool | จริง/เท็จ | true, false |
Object | type พื้นฐานของทุกค่า | อะไรก็ได้ |
Generic Types
List<String> hobbies = ['Cooking', 'Sports']; // กล่องใส่ String
List<Color> colors = [Colors.green, Colors.red]; // กล่องใส่ Colorตัวแปร: var, final, const
var | final | const | |
|---|---|---|---|
| เปลี่ยนค่าได้ไหม | ได้ | ไม่ได้ | ไม่ได้ |
| รู้ค่าตอนไหน | ตอนรัน | ตอนรัน | ตอน compile |
| เทียบ Java | ตัวแปรธรรมดา | final | static final + compile-time constant |
var x = Alignment.topLeft; // เปลี่ยนค่าได้
final y = DateTime.now(); // ล็อคค่า แต่รู้ตอนรัน
const z = Alignment.topLeft; // ล็อคค่า + รู้ตั้งแต่ compileกฎ: const > final > var — ใช้ตัวที่เข้มงวดที่สุดเท่าที่ทำได้
Nullable Type
Alignment startAlignment; // ❌ error — ยังไม่มีค่า
Alignment? startAlignment; // ✅ ได้ — เพิ่ม ? บอกว่า null ก็ได้Functions
Positional vs Named Arguments
// Positional — จับคู่ตามลำดับ
void add(a, b) { print(a + b); }
add(5, 10); // a=5, b=10
// Named — จับคู่ตามชื่อ (Flutter ใช้เป็นหลัก)
void add({required a, required b}) { print(a + b); }
add(b: 5, a: 10); // สลับลำดับได้Flutter ใช้ Named Arguments เป็นหลักเพราะ widget มี argument เยอะ:
OutlinedButton(
onPressed: () {}, // named argument
child: Text('Start Quiz'), // named argument
);Functions as Values
ใน Dart function เป็น object — ส่งเป็น argument ได้:
TextButton(
onPressed: rollDice, // ← ส่ง function ไม่ใส่ () → รอ widget เรียก
child: const Text('Roll Dice'),
)เทียบ Java: คล้าย method reference this::rollDice
String Interpolation
var roll = 4;
var path = 'assets/images/dice-$roll.png'; // → "assets/images/dice-4.png"Java ต้องต่อ String ด้วย + แต่ Dart ใช้ $ สะอาดกว่า
Classes
Dart เป็นภาษา OOP — ทุกค่าเป็น Object สร้างจาก Class
class StyledText extends StatelessWidget {
const StyledText(this.text, {super.key}); // constructor + super
final String text; // instance variable
@override
Widget build(BuildContext context) {
return Text(text, style: const TextStyle(fontSize: 28));
}
}Named Constructors
class หนึ่งมี constructor ได้หลายตัว:
GradientContainer(Colors.red, Colors.blue) // constructor ปกติ
GradientContainer.purple() // named constructor
Image.asset('path.png') // Flutter ใช้หลักการเดียวกันเทียบ Java: คล้าย static factory method เช่น List.of(), Optional.empty()
Private ใน Dart
ใช้ _ นำหน้าชื่อ (ไม่มี keyword private เหมือน Java):
class _DiceRollerState extends State<DiceRoller> { ... }
// ↑ private เฉพาะไฟล์นี้การตั้งชื่อ
| สิ่งที่ตั้งชื่อ | รูปแบบ | ตัวอย่าง |
|---|---|---|
| Class | PascalCase | GradientContainer |
| Variable / Function | camelCase | startAlignment |
| File | snake_case | gradient_container.dart |
เปรียบเทียบกับ Java
| ด้าน | Dart | Java |
|---|---|---|
| สร้างโดย | Sun/Oracle | |
| ใช้หลักกับ | Mobile (Flutter) | Backend (Spring Boot) |
| Null safety | มีในตัว (?) | Optional (เวอร์ชันใหม่) |
| Compilation | AOT + JIT | JIT (JVM) |
| สร้าง object | MaterialApp() ไม่ต้อง new | new MaterialApp() |
| Private | _ prefix | private keyword |
| Named constructor | Image.asset() | ไม่มี (ใช้ static factory) |
| String interpolation | 'dice-$roll.png' | "dice-" + roll + ".png" |