Skip to content
DeveloperMemos

Getters and Setters with Dart

Dart, Getters, Setters2 min read

Getters and setters are an essential part of object-oriented programming languages like Dart. They provide control over accessing and modifying class variables, allowing developers to enforce encapsulation and maintain data integrity. In this article, we will delve into the usage of getters and setters in Dart, along with some practical examples.

Getters

A getter is a special method that retrieves the value of a class variable. It allows access to the variable while providing an additional layer of logic if needed. To define a getter in Dart, you use the get keyword followed by the identifier and return type:

1class Person {
2 String _name;
3
4 String get name => _name;
5}

In the example above, we have a Person class with a private _name variable. The getter name returns the value of _name. By using the getter, we can read the value of _name without directly accessing it.

Computed Properties

Getters also enable the creation of computed properties, which are derived from other class variables or computations. Consider the following example:

1class Circle {
2 double _radius;
3
4 double get area => 3.14 * (_radius * _radius);
5}

In this case, we have a Circle class where the area getter calculates and returns the area of the circle based on its radius. The getter allows us to retrieve the area as if it were a regular variable, even though it's computed dynamically.

Setters

While getters are responsible for retrieving values, setters allow modification of class variables. They provide a way to control how variables are updated and add additional logic when needed. To define a setter in Dart, you use the set keyword followed by the identifier and parameter:

1class Person {
2 String _name;
3
4 set name(String value) {
5 // Additional logic, validation, or processing can be added here
6 _name = value;
7 }
8}

In the above example, we have a Person class with a private _name variable. The setter name sets the value of _name based on the provided parameter. This allows us to modify the value of _name through the setter method instead of directly accessing it.

Validation and Encapsulation

Setters enable validation and encapsulation of data within an object. Let's consider a simple scenario where we want to validate and enforce a minimum age requirement for a person:

1class Person {
2 int _age;
3
4 set age(int value) {
5 if (value >= 18) {
6 _age = value;
7 } else {
8 throw ArgumentError('Minimum age requirement not met.');
9 }
10 }
11}

In this case, the setter age validates if the provided value is greater than or equal to 18 before assigning it to _age. If the condition fails, an ArgumentError is thrown, indicating that the minimum age requirement was not met. By using a setter, we ensure that the age property is always set correctly according to our rules.


Getters and setters play a crucial role in controlling access to class variables, enforcing data integrity, and adding custom logic during read and write operations. They allow for encapsulation and provide an additional layer of abstraction over direct variable access. Understanding and utilizing getters and setters effectively can greatly improve code maintainability and flexibility in Dart development.