Boolean trong Dart
🎯 Mục tiêu: Hiểu kiểu bool và các toán tử logic.
💡 Cú pháp cơ bản
bool isActive = true;
bool isCompleted = false;
// Từ biểu thức
bool isAdult = age >= 18;
bool hasPermission = role == "admin";📝 Toán tử so sánh
print(5 == 5); // true
print(5 != 3); // true
print(5 > 3); // true
print(5 >= 5); // true
print(3 < 5); // true🔧 Toán tử logic
bool a = true, b = false;
print(a && b); // false (AND)
print(a || b); // true (OR)
print(!a); // false (NOT)🎯 Short-circuit Evaluation
// && dừng khi gặp false
bool result1 = false && expensiveCheck(); // Không gọi expensiveCheck()
// || dừng khi gặp true
bool result2 = true || expensiveCheck(); // Không gọi expensiveCheck()📱 Trong Flutter
// Visibility
isLoading ? CircularProgressIndicator() : content;
// Button state
ElevatedButton(
onPressed: isValid ? handleSubmit : null,
child: Text("Submit"),
);✅ Checklist
- Sử dụng
boolvớitrue/false - Sử dụng
&&,||,! - Hiểu short-circuit evaluation
Tiếp theo: String
Last updated on