Toán tử logic trong Dart
🎯 Mục tiêu: Sử dụng toán tử logic để kết hợp điều kiện.
💡 Toán tử cơ bản
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 result = false && expensiveCheck();
// || dừng khi gặp true
bool result = true || expensiveCheck();🔧 Kết hợp điều kiện
int age = 25;
bool hasID = true;
bool isVIP = false;
bool canEnter = (age >= 18 && hasID) || isVIP;
print(canEnter); // true📱 Trong Flutter
// Button enabled state
ElevatedButton(
onPressed: (isValid && !isLoading) ? submit : null,
child: Text("Submit"),
);✅ Checklist
- Dùng
&&cho AND - Dùng
||cho OR - Dùng
!cho NOT - Hiểu short-circuit
Last updated on