Dart Getters and Setters

In this tutorial you will learn about the Dart Getters and Setters and its application with practical example.

Dart Getters and Setters

Getters and setters are special class methods that is used to initialize and retrieve the values of class fields respectively. The setter method is used to set or initialize respective class fields, while the getter method is used to retrieve respective class fields. All classes have default getter and setter method associated with it. However, you are free to override the default ones by implementing the getter and setter method explicitly.

Defining a getter

The getters are defined using the get keyword with no parameters and returns a value.

Syntax:-

Defining a setter

The setters are defined using the set keyword with one parameter and no return value.

Syntax:-

Example:-

The following example shows how you can use getters and setters in a Dart class –

Output:-

dart_getters_setters_example

In this tutorial we have learn about the Dart Getters and Setters and its application with practical example. I hope you will like this tutorial.