{"id":19818,"date":"2022-05-27T04:55:15","date_gmt":"2022-05-27T04:55:15","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3872"},"modified":"2022-05-27T04:55:15","modified_gmt":"2022-05-27T04:55:15","slug":"dart-typedef","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/27\/dart-typedef\/","title":{"rendered":"Dart Typedef"},"content":{"rendered":"\n<p>In Dart, the typedef is used to generate an alias for function type that we can use it as type annotation for declaring variables and return types of that function type. An alias of function type can be used as type annotation in variable declaration or function return type. A typedef stores the type information when we assigned the function type to a variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring a typedef<\/h2>\n\n\n\n<p>A&nbsp;<strong>typedef<\/strong>&nbsp;keyword is used to create an alias for function that will be the same as the actual functions. We can also create a function prototype with a list of parameters. 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-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><\/p>\n\n\n\n<ol><li>typedef&nbsp;function_name(parameters)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p>Let&#8217;s create an alias of&nbsp;<strong>MultiOperation(int n1, int n2)<\/strong>&nbsp;that contains two parameters of the integer type.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><\/p>\n\n\n\n<ol><li>typedef&nbsp;MultiOperation(<strong>int<\/strong>&nbsp;n1,&nbsp;<strong>int<\/strong>&nbsp;n2);&nbsp;&nbsp;&nbsp;\/\/&nbsp;function&nbsp;signature&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Assigning typedef Variable<\/h2>\n\n\n\n<p>We can assign any function with the same parameter to the typedef variable. 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-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><\/p>\n\n\n\n<ol><li>type_def&nbsp;var_name&nbsp;=&nbsp;function_name;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the following example, where we define the two functions with the same signature as the&nbsp;<strong>MultiOperation<\/strong>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum(int n1, int n2) {  \n      print(\"Sum of the two number:${n1+n2}\");  \n}  \nSub(int n1, intn2 ) {  \n      print(\"Subtraction of the two number:${n1-n2}\");  \n   \n}  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Calling Function with typedef<\/h2>\n\n\n\n<p>We can invoke function by passing the same parameter by using the&nbsp;<strong>typdef<\/strong>&nbsp;variable. 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-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><\/p>\n\n\n\n<ol><li>var_name(parameter);&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-typedef#\"><\/a><\/p>\n\n\n\n<ol><li>MultiOperation&nbsp;mp;&nbsp;&nbsp;<\/li><li class=\"\">mp&nbsp;=&nbsp;Sum;&nbsp;&nbsp;<\/li><li>mp(20,10);&nbsp;&nbsp;<\/li><li class=\"\">mp&nbsp;=&nbsp;Sub;&nbsp;&nbsp;<\/li><li>mp(30,20);&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>The&nbsp;<strong>mp<\/strong>&nbsp;is a typedef variable, which can be used to refer any method that accepts two integer parameters. The function reference can be switched at runtime by using the typedefs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Program using typedef<\/h2>\n\n\n\n<p>Let&#8217;s have a look at the following example.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef MultiOperation(int num1, int num2);  \/\/ typedef function signature  \nSum(int n1, int n2) {  \n      print(\"Sum of the two number:${n1+n2}\");  \n}  \nSub(int n1, int n2 ) {  \n      print(\"Subtraction of the two number:${n1-n2}\");  \n   \n}  \n  \nvoid main() {  \nMultiOperation mp = Sum;  \nprint(\"Javapoint - Dart typedef Example\");  \n  \nmp(20,10);  \nmp = Sub;  \nmp(30,20);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Javapoint - Dart typedef Example\nSum of the two numbers: 30\nSubtraction of the two numbers: 10\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>In the above code, we created the alias of the&nbsp;<strong>MultiOperation()<\/strong>&nbsp;function using the&nbsp;<strong>typedef<\/strong>&nbsp;keyword. We defined two more functions&nbsp;<strong>Sum()<\/strong>&nbsp;and&nbsp;<strong>Sub<\/strong>(), which have same signature as the typedef function.<\/p>\n\n\n\n<p>Then, we assigned the typedef variable&nbsp;<strong>mp<\/strong>&nbsp;that referred to both functions&nbsp;<strong>Sum()<\/strong>&nbsp;function and&nbsp;<strong>Sub()<\/strong>&nbsp;function. Now, we invoked the function by passing the required argument, and it printed the result to the screen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Typedef as Parameter<\/h2>\n\n\n\n<p>We can use the typedef method as a parameter. In the following example, we are creating an additional function to the above program&nbsp;<strong>NumericOperaion(int n1, int n2, MultiOperation mp)<\/strong>&nbsp;with the two integer variables and typedef&nbsp;<strong>ManyOperation mp<\/strong>&nbsp;as its parameter.<\/p>\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>typedef MultiOperation(int num1, int num2);  \/\/ typedef function signature  \n  \nSum(int n1, int n2) {  \n      print(\"Sum of the two number:${n1+n2}\");  \n}  \nSub(int n1, int n2 ) {  \n      print(\"Subtraction of the two number:${n1-n2}\");  \n}  \n   \nNumericOperation(int n1, int n2, MultiOperation mp){  \n      print(\"Inside Operation\");  \n      mp(n1,n2);  \n         }  \n  \nvoid main() {  \nprint(\"Javapoint - Dart typedef Example\");  \nNumericOperation(20, 10, Sum);  \nNumericOperation(20, 10, Sub);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Javapoint - Dart typedef Example\nInside Operation\nSum of the two number: 30\nInside Operation\nSubtraction of the two number: 10\n<\/code><\/pre>\n\n\n\n<p>In the above code, we didn&#8217;t need to create a typedef variable to the refer to each method; we just called the&nbsp;<strong>NumericOperation()<\/strong>&nbsp;function by passing the required value and typedef variable&nbsp;<strong>mp<\/strong>. It performed the given operations and printed the result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Debugging<\/h2>\n\n\n\n<p>Debugging is the process of identifying and eliminating of existing and possible errors in the Dart program that can cause ambiguity and uncertainty during the program execution. Debugging is essential to detect and fixes the bugs to run the program smoothly or without interruption.<\/p>\n\n\n\n<p>Debugging becomes easier if you are using the IDE for the Dart program. Here we are assuming that you have installed the most common and suitable IDE WebStorme in your system. The WebStorm Editor allows us to step by step debugging.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Breakpoints?<\/h2>\n\n\n\n<p>Breakpoints are the checkpoint of the program to break the program at a specific point to checks its behavior. We can add the breakpoints in the program to check the bugs within that specific area.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to add Breakpoints in WebStorm?<\/h2>\n\n\n\n<p>We can add the breakpoints in WebStorm by simply click on a line number in the left bar to add a breakpoint. After adding the breakpoints, run the program in the debug mode, it will give the Debugger window where we can verify that how the breakpoint works. We can also change the values and see the difference in the watches window.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/dart\/images\/dart-typedef.png\" alt=\"Dart Typedef\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In Dart, the typedef is used to generate an alias for function type that we can use it as type annotation for declaring variables and return types of that function type. An alias of function type can be used as type annotation in variable declaration or function return type. A typedef stores the type information [&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\/19818"}],"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=19818"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/19818\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=19818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=19818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=19818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}