{"id":19821,"date":"2022-05-27T05:13:06","date_gmt":"2022-05-27T05:13:06","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3879"},"modified":"2022-05-27T05:13:06","modified_gmt":"2022-05-27T05:13:06","slug":"dart-generics","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/27\/dart-generics\/","title":{"rendered":"Dart Generics"},"content":{"rendered":"\n<p>Dart Generics are the same as the Dart collections, which are used to store the homogenous data. As we discussed in the Dart features, it is an&nbsp;<strong>optionally typed language<\/strong>.<\/p>\n\n\n\n<p>By default, Dart Collections are the heterogeneous type. In other words, a single Dart collection can hold the values of several data types. However, a Dart collection can be also stored the homogenous values or same type values.<\/p>\n\n\n\n<p>The Dart Generics provides the facility to enforce a limit on the data type of the values that can be stored by the collection. These collections can be referred to as the&nbsp;<strong>type-safe collections<\/strong>.<\/p>\n\n\n\n<p>Type safety is a unique feature of the Dart programming, which makes sure that a memory block can only contain the data of a specific data type.<\/p>\n\n\n\n<p>The generics are a way to support type-safety implementation for all Dart collections. The pair of the angular bracket is used to declare the type-safe collection. The angular bracket consists of the data-types of the collection. The syntax is given below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax &#8211;<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><\/p>\n\n\n\n<ol><li>Collection_name&nbsp;&lt;data_type&gt;&nbsp;identifier&nbsp;=&nbsp;<strong>new<\/strong>&nbsp;Collection_name&lt;data_type&gt;&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>We can do the type-safe implementation of various Dart objects such as List, Queue, Map, and Set. It is also supported by all implementation of the above define collection types. The syntax is given below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211; Generics List<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   List &lt;String> logStr = new List &lt;String>();   \n   logStr.add(\"CHECK\");   \n   logStr.add(\"ERROR\");   \n   logStr.add(\"INFO\");   \n    \n   \/\/iterating across list   \n   for (String i in logStr) {   \n      print(i);   \n   }   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CHECK\nERROR\nINFO\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>We created a list that holds the string type-safe and used the add element into it by using&nbsp;<strong>add()<\/strong>&nbsp;function.<\/p>\n\n\n\n<p>If we try to insert the other than the specified value then it will through a compilation error. Let&#8217;s understand the following example &#8211;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211; 2<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   List &lt;String> logStr = new List &lt;String>();   \n   logStr.add(511);   \/\/ Add integer value  \n   logStr.add(\"ERROR\");   \n   logStr.add(\"INFO\");   \n    \n   \/\/iterating across list   \n   for (String i in logTypes) {   \n      print(i);   \n   }   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>generics.dart:3:17: Error: The argument type 'int' can't be assigned to the parameter type 'String'.\nlogTypes.add(511);\n<\/code><\/pre>\n\n\n\n<p>Let&#8217;s understand another example &#8211;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211; Generic Set<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   Set &lt;int>numberSet = new  Set&lt;int>();   \n   numberSet.add(10);   \n   numberSet.add(20);   \n   numberSet.add(30);   \n   numberSet.add(40);  \n   numberSet.add(50);   \n     \n   \/\/ numberSet.add(\"\");   \n  \/\/ compilation error;   \n   print(\"Default implementation  :${numberSet.runtimeType}\");    \n     \n   for(var i in numberSet) {   \n      print(i);   \n   }   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30\n40\n50\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211; Generics Queue<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'dart:collection';   \nvoid main() {   \n   Queue&lt;int> queue = new Queue&lt;int>();   \n   print(\"Default implementation ${queue.runtimeType}\");    \n   queue.addLast(100);   \n   queue.addLast(205);   \n   queue.addLast(315);   \n   queue.addLast(470);   \n   \/\/ Remove the first element of queue   \n   queue.removeFirst();    \n     \n   for(int i in queue){   \n      print(i);   \n   }   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Default implementation ListQueue&lt;int&gt;\n205\n315\n470\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Generic Map<\/h3>\n\n\n\n<p>As we know that declaring map require the key and value. The syntax is given below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-generics#\"><\/a><\/p>\n\n\n\n<ol><li>Map&nbsp;&lt;Key_type,&nbsp;value_type&gt;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   Map &lt;String, String>m={'name':'Joseph','Rollno':'Std1001'};   \n   print('Map :${m}');   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map :{name: Joseph, Rollno: Std1001}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dart Generics are the same as the Dart collections, which are used to store the homogenous data. As we discussed in the Dart features, it is an&nbsp;optionally typed language. By default, Dart Collections are the heterogeneous type. In other words, a single Dart collection can hold the values of several data types. However, a Dart [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[658],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/19821"}],"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=19821"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/19821\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=19821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=19821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=19821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}