In Dart, getters and setters are special methods that are used to access and modify the private properties of a class. They provide controlled access to a class's properties, allowing for encapsulation and ensuring data integrity.
A getter is used to retrieve or access the value of a private field. You use the get keyword to define a getter method.
Type get propertyName {
// return value;
}
class Student {
String _name; // Private variable (indicated by _)
Student(this._name);
// Getter for _name
String get name {
return _name;
}
}
void main() {
var student = Student('John');
print(student.name); // Access the _name property via the getter
}
A setter is used to modify or update the value of a private field. You use the set keyword to define a setter method.
set propertyName(Type value) {
// Assign value;
}
class Student {
String _name; // Private variable (indicated by _)
Student(this._name);
// Setter for _name
set name(String newName) {
_name = newName;
}
}
void main() {
var student = Student('John');
student.name = 'Alice'; // Update the _name property via the setter
print(student._name); // Prints: Alice
}
You can define both a getter and a setter for the same property to control how it's accessed and updated.
class Student {
String _name;
Student(this._name);
// Getter
String get name => _name;
// Setter
set name(String newName) {
if (newName.isNotEmpty) {
_name = newName;
} else {
print('Name cannot be empty!');
}
}
}
void main() {
var student = Student('John');
print(student.name); // Access using getter
student.name = 'Alice'; // Update using setter
print(student.name); // Prints: Alice
student.name = ''; // Invalid update
}
Encapsulation: Getters and setters enable you to encapsulate private fields.
Validation: Setters can include validation logic to ensure the field is updated with valid values.
Simplified Syntax: Dart also supports simplified getter and setter syntax using the => (arrow function).
In Dart, default getters and setters are automatically created for non-private instance variables of a class. These provide simple access to retrieve or modify the value of the variable without needing to explicitly define custom getter and setter methods.
If you define a public instance variable, Dart implicitly generates a getter to access its value and a setter (if the variable is not final or const) to modify its value.
These default methods are created automatically, so you don't need to define them unless you want to add custom behavior.
class Student {
String name = 'John'; // Public instance variable
}
void main() {
var student = Student();
// Using the default getter
print(student.name); // Output: John
// Using the default setter
student.name = 'Alice';
print(student.name); // Output: Alice
}
Here:
Getter: Automatically created to retrieve name.
Setter: Automatically created to modify name.
If a variable is private (i.e., its name starts with _), Dart does not create public getters or setters. You must define custom ones to access or modify such fields.
Example:
class Student {
String _name = 'John'; // Private variable
}
void main() {
var student = Student();
// student._name; // Error: Can't access private variable directly
}
To access private variables, you must manually create getters and setters.
If a variable is declared as final or const, Dart generates only a default getter but no setter (making it immutable).
Example:
class Student {
final String name = 'John'; // Read-only variable
}
void main() {
var student = Student();
// Using the default getter
print(student.name); // Output: John
// student.name = 'Alice'; // Error: Cannot assign to the final variable 'name'
}
While the default ones are simple, custom getters and setters are useful when you want to:
Add validation (e.g., ensure input values meet certain criteria).
Perform additional operations during access or modification.
Example:
class Student {
String _name = 'John';
// Custom getter
String get name => _name;
// Custom setter with validation
set name(String newName) {
if (newName.isNotEmpty) {
_name = newName;
} else {
print('Name cannot be empty!');
}
}
}
void main() {
var student = Student();
print(student.name); // Access using custom getter
student.name = ''; // Invalid update, triggers validation
student.name = 'Alice'; // Valid update
print(student.name);
}
Default Getters and Setters:
Automatically provided for public instance variables.
Work without needing explicit definition.
Can only perform simple access and assignment.
Custom Getters and Setters:
Necessary for private variables.
Allow control over access and modification.