{"id":3580,"date":"2022-05-19T05:55:19","date_gmt":"2022-05-19T05:55:19","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3580"},"modified":"2022-05-19T05:55:19","modified_gmt":"2022-05-19T05:55:19","slug":"dart-map","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/19\/dart-map\/","title":{"rendered":"Dart Map"},"content":{"rendered":"\n<p>Dart Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type. In Dart Map, each key must be unique, but the same value can occur multiple times. The Map representation is quite similar to Python Dictionary. The Map can be declared by using curly braces {} ,and each key-value pair is separated by the commas(,). The value of the key can be accessed by using a square bracket([]).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring a Dart Map<\/h2>\n\n\n\n<p>Dart Map can be defined in two methods.<\/p>\n\n\n\n<ul><li>Using Map Literal<\/li><li>Using Map Constructor<\/li><\/ul>\n\n\n\n<p>The syntax of declaring Dart Map is given below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Map Literals<\/h3>\n\n\n\n<p>To declare a Map using map literal, the key-value pairs are enclosed within the curly braces &#8220;{}&#8221; and separated by the commas. 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-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var map_name = {key1:value1, key2:value2 &#91;.......,key_n: value_n]}  <\/code><\/pre>\n\n\n\n<p><strong>Example &#8211; 1:<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<ol><li><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   var student = {'name':'Tom','age':'23'};   \n   print(student);   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{name: Tom, age: 23}\n<\/code><\/pre>\n\n\n\n<p><strong>Example &#8211; 2: Adding value at runtime<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   var student = {'name':' tom', 'age':23};   \n   student&#91;'course'] = 'B.tech';   \n   print(student);   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{name: tom, age: 23, course: B.tech}\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above example, we declared a Map of a&nbsp;<strong>student<\/strong>&nbsp;name. We added the value at runtime by using a square bracket and passed the new key as a&nbsp;<strong>course<\/strong>&nbsp;associated with its value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Map Constructor<\/h3>\n\n\n\n<p>To declare the\u00a0Dart\u00a0Map using map constructor can be done in two ways. First, declare a map using\u00a0<strong>map()<\/strong>\u00a0constructor. Second, initialize the map. 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-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var map_name = new map()  <\/code><\/pre>\n\n\n\n<p>After that, initialize the values.<a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<ol><li>map_name[key]&nbsp;=&nbsp;value&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Example &#8211; 1: Map constructor<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   var student = new Map();   \n   student&#91;'name'] = 'Tom';   \n   student&#91;'age'] = 23;   \n   student&#91;'course'] = 'B.tech';   \n   student&#91;'Branch'] = 'Computer Science';  \n   print(student);   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{name: Tom, age: 23, course: B.tech, Branch: Computer Science}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; A map value can be any object including NULL.<\/h4>\n\n\n\n<h2 class=\"wp-block-heading\">Map Properties<\/h2>\n\n\n\n<p>The dart:core:package has Map class which defines following properties.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Properties<\/th><th>Explanation<\/th><\/tr><tr><td>Keys<\/td><td>It is used to get all keys as an iterable object.<\/td><\/tr><tr><td>values<\/td><td>It is used to get all values as an iterable object.<\/td><\/tr><tr><td>Length<\/td><td>It returns the length of the Map object.<\/td><\/tr><tr><td>isEmpty<\/td><td>If the Map object contains no value, it returns true.<\/td><\/tr><tr><td>isNotEmpty<\/td><td>If the Map object contains at least one value, it returns true.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<ol><li><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   var student = new Map();   \n   student&#91;'name'] = 'Tom';   \n   student&#91;'age'] = 23;   \n   student&#91;'course'] = 'B.tech';   \n   student&#91;'Branch'] = 'Computer Science';  \n   print(student);   \n  \n  \/\/ Get all Keys  \n  print(\"The keys are : ${student.keys}\");  \n  \n \/\/ Get all values  \n print(\"The values are : ${student.values}\");  \n   \n \/\/ Length of Map  \n print(\"The length is : ${student.length}\");  \n  \n\/\/isEmpty function  \nprint(student.isEmpty);  \n  \n\/\/isNotEmpty function  \nprint(student.isNotEmpty);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{name: Tom, age: 23, course: B.tech, Branch: Computer Science}\nThe keys are : (name, age, course, Branch)\nThe values are : (Tom, 23, B.tech, Computer Science)\nThe length is : 4\nfalse\ntrue \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Map Methods<\/h2>\n\n\n\n<p>The commonly used methods are given below.<\/p>\n\n\n\n<p><strong>addAll() &#8211;<\/strong>&nbsp;It adds multiple key-value pairs of other. 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-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map.addAll(Map&lt;Key, Value> other)  <\/code><\/pre>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul><li><strong>other &#8211;<\/strong>&nbsp;It denotes a key-value pair. It returns a void type.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   Map student = {'name':'Tom','age': 23};   \n   print('Map :${student}');   \n     \n   student.addAll({'dept':'Civil','email':'tom@xyz.com'});   \n   print('Map after adding  key-values :${student}');   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map :{name: Tom, age: 23}\nMap after adding  key-values :{name: Tom, age: 23, dept: Civil, email: tom@xyz.com}\n<\/code><\/pre>\n\n\n\n<p><strong>remove() &#8211;<\/strong>&nbsp;It eliminates all pairs from the map. 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-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map.clear()  <\/code><\/pre>\n\n\n\n<p>Let&#8217;s have a look at following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   Map student = {'name':'Tom','age': 23};   \n   print('Map :${student}');   \n     \n   student.clear();   \n   print('Map after removing all key-values :${student}');   \n    \n}  <\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map :{name: Tom, age: 23}\nMap after removing all key-values :{}\n<\/code><\/pre>\n\n\n\n<p><strong>remove() &#8211;<\/strong>&nbsp;It removes the key and its associated value if it exists in the given map. 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-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map.remove(Object key)  <\/code><\/pre>\n\n\n\n<p><strong>Parameter &#8211;<\/strong><\/p>\n\n\n\n<ul><li><strong>Keys &#8211;<\/strong>&nbsp;It deletes the given entries. It returns the value associated with the specified key.<\/li><\/ul>\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-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   Map student = {'name':'Tom','age': 23};   \n   print('Map :${student}');   \n     \n   student.remove('age');   \n   print('Map after removing given key :${student}');   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map :{name: Tom, age: 23}\nMap after removing given key :{name: Tom}\n<\/code><\/pre>\n\n\n\n<p><strong>forEach() &#8211;<\/strong>&nbsp;It is used to iterate the Map&#8217;s entries. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-map#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map.forEach(void f(K key, V value));  \n&lt;\/pre>&lt;\/div>  \n&lt;p>&lt;strong>Parameter -&lt;\/strong>&lt;\/p>  \n&lt;ul class=\"points\">  \n&lt;li>&lt;strong>f(K key, V value) -&lt;\/strong> It denotes the key-value pair of the map.&lt;\/li>  \n&lt;\/ul>  \n&lt;p>Let's understand the following example.&lt;\/p>  \n&lt;p>&lt;strong>Example -&lt;\/strong>&lt;\/p>  \n&lt;div class=\"codeblock\">&lt;textarea name=\"code\" class=\"java\">  \nvoid main() {   \n   Map student = {'name':'Tom','age': 23};   \n   print('Map :${student}');   \n   student.forEach((k,v) => print('${k}: ${v}'));   \n     \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map :{name: Tom, age: 23}\nname: Tom\nage: 23<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dart Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type. In Dart Map, each key must be unique, but the same value can occur multiple times. [&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\/3580"}],"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=3580"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3580\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}