Getters và Setters
🎯 Mục tiêu: Control access với getters/setters.
💡 Cú pháp
Phần tiêu đề “💡 Cú pháp”class Rectangle { double width, height;
Rectangle(this.width, this.height);
// Getter double get area => width * height;
// Setter set area(double value) { width = sqrt(value); height = sqrt(value); }}
var rect = Rectangle(10, 20);print(rect.area); // 200.0rect.area = 100; // width = height = 10📝 Private + Getter/Setter
Phần tiêu đề “📝 Private + Getter/Setter”class User { int _age = 0;
int get age => _age;
set age(int value) { if (value >= 0 && value <= 150) { _age = value; } }}📱 Trong Flutter
Phần tiêu đề “📱 Trong Flutter”class CounterState { int _count = 0;
int get count => _count;
void increment() => _count++; void decrement() => _count--;}✅ Checklist
Phần tiêu đề “✅ Checklist”-
getcho computed properties -
setcho validation -
_prefix cho private