{"id":3920,"date":"2022-05-31T07:11:18","date_gmt":"2022-05-31T07:11:18","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3920"},"modified":"2022-05-31T07:11:18","modified_gmt":"2022-05-31T07:11:18","slug":"dart-getters-and-setters","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/31\/dart-getters-and-setters\/","title":{"rendered":"Dart Getters and Setters"},"content":{"rendered":"\n<p>Getters and setters are the special class method that is used to read and write access to an object&#8217;s properties. The getter method is used to reads the value of the variable or retrieve the value and setter method is used to set or initialize respective class fields. By default, all classes are associated with getter and setter method. However, we can override the default methods by defining getter and setter method explicitly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining a getter<\/h2>\n\n\n\n<p>We can define the getters method by using the get keyword with no parameter a valid return type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-getters-and-setters#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-getters-and-setters#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-getters-and-setters#\"><\/a><\/p>\n\n\n\n<ol><li>return_type&nbsp;get&nbsp;field_name{&nbsp;&nbsp;<\/li><li class=\"\">}&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Defining a setter<\/h2>\n\n\n\n<p>We can declare the setter method using the set keyword with one parameter and without return type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-getters-and-setters#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-getters-and-setters#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-getters-and-setters#\"><\/a><\/p>\n\n\n\n<ol><li>set&nbsp;field_name&nbsp;{&nbsp;&nbsp;<\/li><li class=\"\">}&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {  \n          String stdName;  \n          String branch;  \n          int stdAge;  \n \/\/ getter method   \n         String get std_name   \n             {  \n             return stdName;  \n             }  \n         void set std_name(String name)  \n                 {  \n              this.stdName = name;  \n  \n               }  \n          void set std_age(int age) {  \n               if(age> = 20){  \n                   print(\"Student age should be greater than 20\")  \n                     }else{   \n                         this.stdAge = age;  \n                              }  \n                      }  \n  \n                                                         }  \n           int get std_age{  \n                 return stdAge;  \n  \n}  \n        void set std_branch(String branch_name) {  \n                 this.branch = branch_name;  \n  \n}  \n     int get std_branch{  \n            return branch;  \n}  \n  \n}  \nvoid main(){  \nStudent std = new Student();  \nstd.std_name = 'John';  \nstd.std_age = 24;  \nstd.std_branch = 'Computer Science';  \n  \nprint(\"Student name is: ${std_name}\");  \nprint(\"Student age is: ${std_age}\");  \nprint(\"Student branch is: ${std_branch}\");  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student name is: John\nStudent age is: 24\nStudent Branch is: Computer Science\n<\/code><\/pre>\n\n\n\n<p>We can also place the getter and setter method just after the. Now, let&#8217;s understand the following example:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211; 2<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car {  \n  String makedate;  \n  String modelname;  \n  int manufactureYear;  \n  int carAge;  \n  String color;  \n\/\/ Getter method  \n  int get age {  \n    return carAge;  \n  }  \n\/\/ Setter Method  \n  void set age(int currentYear) {  \n    carAge = currentYear - manufactureYear;  \n  }  \n\/\/ defining parameterized constructor  \n  Car({this.makedate,this.modelname,this.manufactureYear,this.color,});  \n}  \n\/\/Age here is both a getter and a setter. Let's see how we can use it.  \nvoid main() {  \n Car c =   \n Car(makedate:\"Renault 20\/03\/2010\",modelname:\"Duster\",manufactureYear:2010,color:\"White\");  \n  print(\"The car company is: ${c.makedate}\");   \n  print(\"The modelname is: ${c.modelname}\");   \n  print(\"The color is:${c.color}\");  \n  c.age = 2020;  \n  print(c.age);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The car company is: Honda 20\/03\/2010\nThe modelname is: City\nThe color is: White\n10\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>In the above code, we defined the getter and setter methods before the constructor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getters and setters are the special class method that is used to read and write access to an object&#8217;s properties. The getter method is used to reads the value of the variable or retrieve the value and setter method is used to set or initialize respective class fields. By default, all classes are associated with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[340],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3920"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3920"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3920\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}