Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are the setter and getter methods in Dart?

Maria Elijah

The class methods, getter and setter, are used to manipulate the data of the class attributes. A setter is used to set the data of the fieldName class to some variable, whereas a getter is used to read or get the data of the fieldName class.

Setter method

The setter method is used to set the data of a variable returned by the getter method. A default setter function exists in all classes, although it can be overridden explicitly.

The set keyword can be used to define the setter method.

returnType set fieldName {
   // set the value

Getter method

A getter method is used to get and save a specific fieldName class in a variable. A default getter method exists in all classes, but it can be overridden explicitly. The get keyword can be used to define the getter method.

returnType get fieldName {
   // return the value


Below is a code sample for the setter and getter method.

// implementing getter and setter method

// Creating class 
class Educative {
  // Creating a field
  String shotTitle;
  // Using the getter method to get input
  String get shot_title {
    return shotTitle;
  // Using the setter method to set the input
  set shot_title (String title) {
    this.shotTitle = title;
void main() 
    // Creating Instance of a class 
    Educative edu = new Educative(); 
  // using class instance to access the class  getter method
  // getter method to get the name
    edu.shot_title = "Dart - Setter and Getter methods";
  // Print the input 
  print("This is an Educative shot on ${edu.shot_title}");



View all Courses

Keep Exploring