Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias of the function type can be used as an annotation type in a variable declaration or a return function type. Typedef stores type information when assigning a function type to a variable.

The typedef keyword is used to create an alias for function that will be the same as the actual functions. We can also create a prototype of the function with a list of parameters. The syntax is provided below.

Syntax :

typedef function_name( parameters )  

Example -

Let's create an alias of func( int f1, int f2 ) that contains two parameters of the integer type.

Typedef func( int f1, int f2 ) ;   // function signature 

Assigning typedef Variable

We may assign any function with the same parameter for typedef variable. The syntax is provided below.

Syntax:

type_def var_name = function_name( ) ; 

Let’s understand the following example, where we describe two functions with the same signature as func( ).

Sum( int a, int b ) {  
      print( " Sum of the two numbers is : ${ a + b } " ) ;  
}  
Sub( int c , int d ) {  
      print( " Subtraction of the two numbers is : ${ c - d } " ) ;  
}  

Calling Function with typedef

We can request a function by passing the same parameter using the typdef variable. The syntax is provided below.

Syntax:

var_name( parameter ) ; 

Example :

func f ;  
f = Sum ;  
f( 5, 10 ) ;  
f = Sub ;  
f( 15, 20 ) ;  

‘ f ’ is a typedef variable, which can be used to refer to any method that accepts two integer parameters. The function reference can be changed during operation using typedefs.

Complete Program using typedef

Let's have a look at the following example.

typedef func( int n1, int n2 ) ;  // typedef function signature  
Sum( int a, int b ) {  
      print( " Sum of the two numbers is : ${ a + b } " ) ;  
}  
Sub( int c, int d ) {  
      print( " Subtraction of the two numbers is : ${ c – d } " ) ;  
}  
void main( ) {  
func f = Sum ;  
print( " This program depicts an example of typedefs in Dart " ) ;  
f( 5 ,10 ) ;  
f = Sub ;  
f( 3 , 20 ) ;  
}  

Output :

This program depicts an example of typedefs in Dart 
Sum of the two numbers : 15
Subtraction of the two numbers : 23

Explanation :

In the code above, we created an alias for the ‘ func( ) ’ function using a typedef keyword. We have described two additional functions ‘ Sum( ) ’ and ‘ Sub( ) ’, which have the same signature function as typedef function.

Then, assign a variable type of typedef ‘ f ’ referring to both the ‘ Sum( ) ’ function and the ‘ Sub( ) ’ function. Now, we invoked the function by passing the required argument, and it printed the result on the screen.

Typedef as Parameter

We can use typedef method as a parameter. In the following example, we create additional function in the above program func1( int n1, int n2, func f ) with two-integer variables and typedef func f as its parameter.

Example -

typedef func( int f1, int f2 ) ;  // declaration of the typedef function func( )
Sum( int n1, int n2 ) {  
      print( " Sum of the two numbers is : ${ n1 + n2 } " ) ;  
}  
Sub( int n1, int n2 ) {  
      print( " Subtraction of the two numbers is : ${ n1 - n2 } " ) ;  
}     
func1( int n1, int n2, func f ) {  
      print( " Inside the func1( ) function " ) ;  
      f( n1, n2 ) ;  
         }  
void main( ) {  
print( " " ) ;  
func1( 20, 10, Sum ) ;  
func1( 20, 10, Sub ) ;  
} 

Output :

This program depicts an example of typedefs in Dart 
Inside the func1( ) function 
Sum of the two number : 30
Inside the func1( ) function 
Subtraction of the two number : 10

Dart Debugging

In the code above, we did not create a typedef variable to refer to each method; we just called the ‘ func1( ) ’ function by passing the required value and variation of typedef mp. It Performed the given operations and printed the result.

Debugging is a process of identifying and eliminating existing and possible errors in the Dart program that can cause ambiguity and uncertainty during the program execution. Debugging is essential to find and fix the bugs to run the program smoothly or without interruption.

Debugging is easier when you use the IDE for the Dart program. Here we assume that you have installed the most common and appropriate IDE WebStorme in your system. WebStorm Editor allows us to debug the error step by step.

What are Breakpoints?

Breakpoints are the check points of the program to break the system at a particular point to test its behaviour. We can add breakthroughs to the program to check for bugs in that particular area.

How to add Breakpoints in WebStorm?

We can add breakthroughs to WebStorm by simply clicking on the line number in the left bar to add a breakpoint. After adding the breakpoint, run the program in debug mode, it will provide a Debugger window where we can verify how the breakpoint works. And we can change values and see the difference in the watches window.