Dart : Why do we use getter & setter

Dart : Why do we use getter & setter

·

1 min read

Use Of Getter and Setter :

  • validate the data before reading or writing.

  • Restrict the read and write access to the properties.

  • Making the properties read-only or write-only.

  • Perform some action before reading or writing the properties.

When To Use Getter And Setter :

  • Use getter and setter when you want to restrict the access to the properties.

  • Use getter and setter when you want to perform some action before reading or writing the properties.

  • Use getter and setter when you want to validate the data before reading or writing the properties.

  • Don’t use getter and setter when you want to make the properties read-only or write-only.