Menu
Coddy logo textTech

Getters and Setters

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 21 of 110.

Getters and setters provide a way to access and modify class fields through special methods that look like regular properties. Instead of exposing fields directly, you can control how values are read and written.

A getter is defined using the get keyword and returns a computed or stored value:

class Rectangle {
  double width;
  double height;
  
  Rectangle(this.width, this.height);
  
  double get area => width * height;
  double get perimeter => 2 * (width + height);
}

Rectangle rect = Rectangle(5, 3);
print(rect.area);      // 15.0
print(rect.perimeter); // 16.0

Notice that area and perimeter are accessed like fields, but they're calculated each time. No parentheses needed.

A setter uses the set keyword and allows you to control how a value is assigned:

class Temperature {
  double _celsius;
  
  Temperature(this._celsius);
  
  double get celsius => _celsius;
  set celsius(double value) {
    if (value >= -273.15) {
      _celsius = value;
    }
  }
  
  double get fahrenheit => _celsius * 9 / 5 + 32;
  set fahrenheit(double value) {
    celsius = (value - 32) * 5 / 9;
  }
}

Temperature temp = Temperature(25);
print(temp.fahrenheit);  // 77.0
temp.fahrenheit = 100;
print(temp.celsius);     // 37.78

The setter validates input before storing it and can even convert between units. This gives you control over your data while keeping a clean, property-like syntax for users of your class.

challenge icon

Challenge

Easy

Let's build a score tracker for a game that uses getters and setters to manage player statistics with validation and computed values.

You'll organize your code into two files:

  • player.dart: Define a Player class that tracks a player's game performance. Your class should have:
    • A String name field for the player's name
    • A private int _score field starting at 0
    • A private int _lives field starting at 3
    • A constructor that takes the player's name
    • A getter score that returns the current score
    • A setter score that only accepts values of 0 or greater (ignore negative values)
    • A getter lives that returns the current lives
    • A setter lives that only accepts values between 0 and 5 inclusive (ignore values outside this range)
    • A getter level that computes the player's level based on score: score divided by 100 (integer division), plus 1
    • A getter isAlive that returns true if lives is greater than 0
    • A displayStatus() method that prints the player's current status
  • main.dart: Import your player class and demonstrate the getters and setters:
    • Create a player named 'Alex'
    • Set the score to 250 and call displayStatus()
    • Try to set the score to -50 (should be ignored) and call displayStatus()
    • Set lives to 1 and call displayStatus()
    • Try to set lives to 10 (should be ignored) and call displayStatus()

The displayStatus() method should print in this exact format:

[name] | Score: [score] | Level: [level] | Lives: [lives] | Alive: [isAlive]

Expected output:

Alex | Score: 250 | Level: 3 | Lives: 3 | Alive: true
Alex | Score: 250 | Level: 3 | Lives: 3 | Alive: true
Alex | Score: 250 | Level: 3 | Lives: 1 | Alive: true
Alex | Score: 250 | Level: 3 | Lives: 1 | Alive: true

Cheat sheet

Getters and setters provide controlled access to class fields through special methods that look like regular properties.

Getter - defined using the get keyword, returns a computed or stored value:

class Rectangle {
  double width;
  double height;
  
  Rectangle(this.width, this.height);
  
  double get area => width * height;
  double get perimeter => 2 * (width + height);
}

Rectangle rect = Rectangle(5, 3);
print(rect.area);      // 15.0 - accessed like a field, no parentheses
print(rect.perimeter); // 16.0

Setter - uses the set keyword, controls how values are assigned:

class Temperature {
  double _celsius;
  
  Temperature(this._celsius);
  
  double get celsius => _celsius;
  set celsius(double value) {
    if (value >= -273.15) {
      _celsius = value;
    }
  }
  
  double get fahrenheit => _celsius * 9 / 5 + 32;
  set fahrenheit(double value) {
    celsius = (value - 32) * 5 / 9;
  }
}

Temperature temp = Temperature(25);
print(temp.fahrenheit);  // 77.0
temp.fahrenheit = 100;
print(temp.celsius);     // 37.78

Setters can validate input and convert between units while maintaining clean, property-like syntax.

Try it yourself

import 'player.dart';

void main() {
  // TODO: Create a player named 'Alex'

  // TODO: Set the score to 250 and call displayStatus()

  // TODO: Try to set the score to -50 (should be ignored) and call displayStatus()

  // TODO: Set lives to 1 and call displayStatus()

  // TODO: Try to set lives to 10 (should be ignored) and call displayStatus()
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming