{"id":3570,"date":"2022-05-19T05:24:51","date_gmt":"2022-05-19T05:24:51","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3570"},"modified":"2022-05-19T05:24:51","modified_gmt":"2022-05-19T05:24:51","slug":"dart-constants","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/19\/dart-constants\/","title":{"rendered":"Dart Constants"},"content":{"rendered":"\n<p>Dart Constant is defined as an immutable object, which means it can&#8217;t be changed or modified during the execution of the program. Once we initialize the value to the constant variable, it cannot be reassigned later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining\/Initializing Constant in Dart<\/h3>\n\n\n\n<p>The Dart constant can be defined in the following two ways.<\/p>\n\n\n\n<ul><li>Using the final keyword<\/li><li>Using the const keyword<\/li><\/ul>\n\n\n\n<p>It is beneficial when we want to keep the value unchanged in the whole program. The keywords final and const are used to create a constant variable. Both keywords final and const are used as a conjunction with the data-type. Dart will throw an exception if we try to modify the constant variable.<\/p>\n\n\n\n<p>A const keyword represents the compile-time constant, and the final variable can be set only once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Define Constant Using final Keyword<\/h3>\n\n\n\n<p>We can define the constant by using the final keyword. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<ol id=\"block-a511e71c-d306-4b67-895a-e343238c59d6\"><li><strong>final<\/strong>&nbsp;const_name;&nbsp;&nbsp;<\/li><li>or&nbsp;&nbsp;&nbsp;<\/li><li><strong>final<\/strong>&nbsp;data_type&nbsp;const_name&nbsp;<\/li><\/ol>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><\/p>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main () {  \n  final a = 10;  \n  final b = 20;  \n    \n print(a);  \n print(b);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Define Constants Using const Keyword<\/h3>\n\n\n\n<p>We can define constant using the const keyword. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const const_name  \nOr  \nconst data_type const_name  <\/code><\/pre>\n\n\n\n<p>Let&#8217;s understand the following example.<a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constants#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n   const name= \"Peter\";  \n   print(name);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Peter\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dart Constant is defined as an immutable object, which means it can&#8217;t be changed or modified during the execution of the program. Once we initialize the value to the constant variable, it cannot be reassigned later. Defining\/Initializing Constant in Dart The Dart constant can be defined in the following two ways. Using the final keyword [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[917],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3570"}],"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=3570"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3570\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}