Author: saqibkhan

  • Generators

    Dart Generator is a unique function that allows us to produce a sequence of values. Generators return values on demand; it means the value is generated when we try to iterate over the iterators. Dart provides built-in support for two types of generator functions.

    • Synchronous Generators
    • Asynchronous Generators

    Synchronous Generator

    It returns an iterable object which carries value synchronously. The yield keyword is used along with marking the synchronous generator function body as sync* to generator values.

    Let’s understand the following example of a synchronous generator.

    Example –

    main()  {  
    
        print("Dart Synchronous Generator Example.");  
    
        oddNumber(10).forEach(print);   
    
    }  
    
        // syn* functions returns an iterable  
    
      
    
       Iterable<int> oddNumber(int num) sync* {  
    
        int k = num;  
    
        while(k >= 0) {  
    
             if(k%2 == 1) {  
    
                // 'yield' statement  
    
                yield k;  
    
      
    
                    }  
    
    k--;  
    
    }  
    
    }

    Output

    Dart Synchronous Generator Example.
    9
    7
    5
    3
    1
    

    Explanation:

    In the above program, we declared an oddNumber(10) function with the foreach loop without its body. The foreach loop will iterate the function. Now, we created oddNumber(10) function as synchronous generator function.

    In the function body, we initialized a new variable k, which assigns the argument n. Then, we applied while loop to iterate the function, and the loop body is iterated until the value of k is less than or equal to 0.

    We did modulus operation on k to find the odd numbers from the generators. We used the yield statement, which suspends the function’s execution and returns the value at a time. It will return the value of each execution of the generator function. When the while loop’s condition becomes false, then the loop terminated and prints the odd numbers.

    Asynchronous Generators

    It returns a stream object which carries value asynchronously. The yield keyword is used along with marking the asynchronous generator function body as async* to generator values.

    Let’s understand the following example –

    Example –

    main()  {  
    
        print("Dart Asynchronous Generator Example.");  
    
        asyncNaturalsTo(10).forEach(print);   
    
    }  
    
        // async* functions returns an stream object  
    
      
    
       Stream<int> asyncNaturalsTo(int num) async* {  
    
        int k = 0;  
    
        while(k < num) {  
    
       
    
                // 'yield' statement  
    
                yield k++;  
    
      
    
                    }  
    
    k--;  
    
    } 

      Output

      Dart Asynchronous Generator Example.
      0
      1
      2
      3
      4
      5
      6
      7
      8
      9
      

      Explanation:

      The above code generated values asynchronously. The asyncNaturalsTo(int num) function returns a stream objects in each execution of function body. Here, the yield keyword behaved the same as the previous example; it stopped the execution of the function, returned the value, and resumed the execution for the next iteration. It will happen again and again until the function body is terminated.

      Let’s understand the following concepts related to the generator function.

      The yield Keyword

      The yield returns the single value to the sequence at a time but does not stop the execution of the generator function completely. It returns a value for each execution of the generator function.

      The sync* Keyword –

      The sync* keyword is used to declare the synchronize generator function. It returns the value when we try to iterator the value not when it was created. Let’s have a look at the following example –

      Example –

      void main() {  
      
        print('creating iterator');  
      
        Iterable<int> numbers = getNumbers(4);  // Here we are creating iterator  
      
        print('Iteration starts...');  
      
        for (int i in numbers) {  
      
          print('$i');        // Iterate over the iterator  
      
        }  
      
        print('end of main function');  
      
      }  
      
      Iterable<int> getNumbers(int n) sync* {            // define generator synchronously  
      
        print('generator started');  
      
        for (int i = 0; i < n; i++) {  
      
          yield i;  
      
        }  
      
        print('generator function ended');  
      
      }  

        Output

        creating iterator
        Iteration starts...
        generator started
        0
        1
        2
        3
        generator function ended
        end of main function
        

        Explanation –

        The above generator function generated the value when we iterate over the iterator.

        The async* Keyword

        The async keyword is used to declare the asynchronous generators. It returns the stream object. Let’s understand the following example –

        Example –

        void main() {  
        
          print('creating iterator');  
        
          Stream<int> numbers = getNumbers(4);  
        
          print('starting to listen...');  
        
          numbers.listen((int k) {  
        
            print('$k');  
        
          });  
        
          print('end of the main function');  
        
        }  
        
        Stream<int> getNumbers(int number) async* {   // declaring asynchronous generator function  
        
          print('waiting inside generator a 3 seconds :)');   
        
          await new Future.delayed(new Duration(seconds: 3)); //sleep 3s  
        
          print('started generating values...');  
        
          for (int i = 0; i < number; i++) {  
        
            await new Future.delayed(new Duration(seconds: 1)); //sleep 1s  
        
            yield i;  
        
          }  
        
          print('ended generating values...');  
        
        } 

          Output

          creating iterator
          starting to listen...
          end of the main function
          waiting inside generator a 3 seconds :)
          started generating values...
          0
          1
          2
          3
          ended generating values...
          
        1. Libraries

          In Dart, the library is the collection of the routine or set of programming instructions. Dart consists of many sets of built-in libraries that are beneficial to hold routines (functions, set of classes, etc.), and regularly used. A Dart library contains constants, functions, properties, exceptions, and typedefs, and set of classes.

          Importing a library

          To work with the library, we must import it into the current program. The Dart provides the import keyword, which is used to make the library available in the current file. We can use multiple libraries in a single file.

          For example – Dart built-in library URIs is used as dart scheme to refer to a library. Other libraries can use a file system path or the package: scheme to specify its URIs. The package manager pub in Dart provides the libraries and uses the package scheme.

          We are describing some commonly used libraries below.

          Sr.LibraryDescription
          1.dart:ioThis library consists of File, HTTP, socket, and other I/O support for server applications. This library is not suitable for browser-based applications. We don’t need to import explicitly because it is imported by default.
          2.Dart:coreThis library consists of Collection, built-in types, and other core functionality for each dart program. It is imported by default.
          3.Dart: mathThis library consists of the rich mathematical functions, constant, and random number generator.
          4.Dart: convertIt is used to Encoders and decoders for converting the different data representations such as JSON and UTF
          5.Dart: typed_dataIt represents the lists that store the fixed-sized data efficiently (for example – unsigned 8-byte integer).

          Let’s understand the following example of importing and using a library function.

          Example – Importing and using a Library

          import 'dart:math';   // Importing built-in library  
          
          void main() {   
          
             print("Square root of 36 is: ${sqrt(36)}");   
          
          } 

            Output:

            Square root of 25 is: 5.0
            

            Explanation:

            In the above code, we imported the built-in library ‘dart:math’. It provides the many built-in mathematical function, here we used the sqrt() function with number. It takes a number as an argument that we want to find its square root of. We passed an integer number 25 in sqrt() function, and it retuned an output as 5.

            Encapsulation in Libraries

            Dart provides the facility to encapsulate or restrict access the content of the dart library. It can be done by using the _(underscore), followed by the identifier. The _(underscore) symbol makes the library’s content completely private. The syntax is given below.

            Syntax:

            _identifier  

            Example –

            We define a library called Greetings that has a private function.

            library Greetings;        
            
            // We define a function using the _underscore as a prefix.                        
            
            void _sayHi(msg) {  
            
               print("We will access this method in another program:${msg}");        
            
            }

            The above file saves as greetings.dart, now we import the library.

            import 'greetings.dart' as w;   
            
            void main() {   
            
               w._sayHi("Hello Javatpoint");   
            
            } 

              Output:

              After running the above code, it throws an error because we have declared the library with the private method and try to access it in other file.

              Unhandled exception:
              No top-level method 'w._sayHi' declared.
              NoSuchMethodError: method not found: 'w._sayHi'
              Receiver: top-level
              Arguments: [...]
              #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:184)
              #1 main (file:///C:/Users/Administrator/WebstormProjects/untitled/Assertion.dart:6:3)
              #2 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261)
              #3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)
              

              Creating Custom Libraries (User-defined Library)

              We can also use our own code as a library and import it when needed. This type of library is called a custom library. Below are the steps to create a custom library.

              Step – 1: Declaring a Library

              The library statement is used to create a library explicitly. The syntax is given below.

              Syntax:

              library library_name    
              
              // library contents go here   

                Step – 2: Connecting a Library

                We can connect a library in two ways.

                • Within the same directory
                import 'library_name'  
                • From a different directory
                import 'dir/library_name'  

                Let’s understand the following example –

                Example – Custom Library

                library calculator_simple;    
                
                import 'dart:math';   
                
                  
                
                //library content  
                
                int add(int num1,int num2){   
                
                   print("inside add method of calculator_simple Library ") ;   
                
                   return num1+num2;   
                
                }    
                
                int multiplication(int num1,int num2){   
                
                   print("inside multiplication method of calculator_simple Library ") ;   
                
                   return num1*num2;   
                
                }    
                
                  
                
                int subtraction(int num1,int num2){   
                
                   print("inside subtraction  method of calculator_simple Library ") ;   
                
                   return num1-num2;   
                
                }    
                
                  
                
                int modulus(int num1,int num2){   
                
                   print("inside modulus  method of calculator_simple Library ") ;   
                
                   return num1%num2;   
                
                }    

                  Now we import the above custom file in current file called ‘library.dart’.

                  import 'calculator.dart';    
                  
                  void main() {  
                  
                     var n1 = 30;   
                  
                     var n2 = 10;   
                  
                     var sum = add(n1,n2);   
                  
                     var mod = modulus(n1,n2);   
                  
                     var mul = multiplication(n1,n2);  
                  
                     var div = divide(n1,n2);  
                  
                     var sub = subtraction(n1,n2);  
                  
                       
                  
                       
                  
                     print("$n1 + $n2 = $sum");   
                  
                     print("$n1 %  $n2= $mod");   
                  
                     print("$n1 + $n2 = $mul");   
                  
                     print("$n1 - $n2 = $sub");  
                  
                       
                  
                  } 

                    Output:

                    inside add method of calculator_simple Library
                    inside modulus method of calculator_simple Library
                    inside multiplication method of calculator_simple Library
                    inside subtraction method of calculator_simple Library
                    30 + 10 = 40
                    30 % 10= 0
                    30 + 10 = 300
                    30 - 10 = 20
                    

                    Copy the above code and paste it into your dart editor and observe the result.

                    Note – The custom library must be imported by its saved file name such as we imported it in the current working file with the calculator_simple name.

                    Name Alias of Library

                    Dart allows us to import multiple libraries into the current working file, but if we create the same function name within the different libraries, it will create conflict while accessing these functions. The Dart compiler might be confused to identify the particular function in different library. To overcome this scenario, Dart provides the as keyword for specifying the prefix. The syntax is given below.

                    Syntax:

                    import 'library_uri' as prefix  

                    Let’s understand the following example –

                    Example –

                    First, we define a library: greeting.dart

                    library greetings;    
                    
                    void sayHi(msg){   
                    
                       print("Learn the Dart with ${msg}");  
                    
                    }

                    Next, we define the new library: hellogreetings.dart

                    library hellogreetings;   
                    
                    void sayHi(msg){   
                    
                       print("${msg} provides the tutorial on all technical retated topic");   
                    
                    }  

                      Now, we import the above libraries with the as prefix.

                      import 'greetings.dart';   
                      
                      import 'hellogreetings.dart' as gret;    
                      
                        
                      
                      // using as prefix avoids function name clashes   
                      
                      void main(){   
                      
                         sayHi("JavaTpoint");   
                      
                         gret.sayHi("JavaTpoint");   // To eliminate the name confliction  
                      
                      }

                      Output:

                      Learn the Dart with JavaTpoint
                      JavaTpoint provides the tutorial on all technical related topic
                      
                    1. Packages

                      Dart package is the collection of a well-organized, independent, and reusable code unit. Applications might be needed to implement third-party libraries or packages. The package generally contains a set of classes, functions, or unit of code for specific tasks along with the compiled program and sample data. Dart provides an extensive set of default packages that load automatically when the dart console starts. However, if we need packages other than the default packages then its need to installed and loaded explicitly in order to use. When a package is loaded, it can be used in the whole Dart environment.

                      Dart Package Manager

                      Every language provides the functionality for handling the external packages such as Nuget for .NET, Gradle or Maven for Java, npm for Node.js, etc. Dart has the inbuilt package manager, which is called a pub. It is basically used to organize, manage the third-party libraries, tools, dependencies, and also used to install the packages in the repository. Every Dart application consists of a pubspec.yaml file which includes the metadata of the file. The metadata of the package holds the author, version, application name, and description. The full form of the yaml is a Yet Another Markup Language. The pubspec.yaml is used to download the various libraries that application needs during programming. The pubspec.yaml file must look like as follows.

                      name: 'vector_victor'   
                      
                      version: 0.0.1   
                      
                      description: An absolute bare-bones web app.   
                      
                      ...   
                      
                      dependencies: browser: '>=0.10.0 <0.11.0'   

                        The Dart IDE’s provides the inbuilt support for using the pub that consist of creating, downloading, updating, and publishing packages, Otherwise we can use the pub command line. The following is the list of the few important pub commands.

                        Sr. NoDescription
                        pub getIt is used to get all packages of application dependent on.
                        pub upgradeIt is used to upgrade all application dependencies to the modern version.
                        pub buildIt is used to construct your web application, and it will create a build folder, with all related scripts in it.
                        pub helpIt is used to get help related to all pub commands or when we stuck while programming.

                        Installing a Package

                        The following steps are defining the installation of the package in the project.

                        Step – 1: Write the package name in the dependencies section of project’s pubspec.yaml file. Then run the below command to find the package installed in project.

                        pub get   

                        The above command will download the package under the packages folder in the application directory.

                        Example –

                        name: TestApp  
                        
                        version: 0.0.1  
                        
                        description: A simple dart application  
                        
                        dependencies:  
                        
                        xml:

                        We have added the xml to the project dependencies. Now we can use Dart XML package in the project by importing it. It can be imported as follows.

                        import 'package:xml/xml.dart' as xml;  

                        Read XML String

                        We can read XML string and authenticate the input; Dart XML provides a parse() method to read the string input. The syntax is given below.

                        xml. parse(String input):  

                        Let’s have a look at the following example:

                        Example – Parsing XML String Input

                        In the following example, we display the parsing XML string input.

                        import 'package:xml/xml.dart' as xml;   
                        
                        void main(){   
                        
                           print("xml");   
                        
                           var bookstoreXml = '''<?xml version = "1.0"?>   
                        
                           <bookstore>   
                        
                              <book>   
                        
                                 <title lang = "English">Who will cry when you die </title>   
                        
                                 <price>150.00</price>   
                        
                              </book>   
                        
                                
                        
                              <book>   
                        
                                 <title lang = "English">The Alchemist </title>   
                        
                                 <price>90.00</price>   
                        
                              </book>   
                        
                              <price>200.00</price>   
                        
                           </bookstore>''';   
                        
                             
                        
                           var document = xml.parse(bookstoreXml);   
                        
                           print(document.toString());   
                        
                        } 

                          Output:

                          xml
                          
                          
                          
                          150.00
                          
                          
                          
                          
                          90.00
                          
                          200.00
                          
                          
                        1. Generics

                          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 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 collection can be also stored the homogenous values or same type values.

                          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 type-safe collections.

                          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.

                          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.

                          Syntax –

                          Collection_name <data_type> identifier = new Collection_name<data_type>   

                          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.

                          Example – Generics List

                          void main() {   
                          
                             List <String> logStr = new List <String>();   
                          
                             logStr.add("CHECK");   
                          
                             logStr.add("ERROR");   
                          
                             logStr.add("INFO");   
                          
                              
                          
                             //iterating across list   
                          
                             for (String i in logStr) {   
                          
                                print(i);   
                          
                             }   
                          
                          } 

                            Output

                            CHECK
                            ERROR
                            INFO
                            

                            Explanation:

                            We created a list that holds the string type-safe and used the add element into it by using add() function.

                            If we try to insert the other than the specified value then it will through a compilation error. Let’s understand the following example –

                            Example – 2

                            void main() {   
                            
                               List <String> logStr = new List <String>();   
                            
                               logStr.add(511);   // Add integer value  
                            
                               logStr.add("ERROR");   
                            
                               logStr.add("INFO");   
                            
                                
                            
                               //iterating across list   
                            
                               for (String i in logTypes) {   
                            
                                  print(i);   
                            
                               }   
                            
                            } 

                              Output

                              generics.dart:3:17: Error: The argument type 'int' can't be assigned to the parameter type 'String'.
                              logTypes.add(511);
                              

                              Let’s understand another example –

                              Example – Generic Set

                              void main() {   
                              
                                 Set <int>numberSet = new  Set<int>();   
                              
                                 numberSet.add(10);   
                              
                                 numberSet.add(20);   
                              
                                 numberSet.add(30);   
                              
                                 numberSet.add(40);  
                              
                                 numberSet.add(50);   
                              
                                   
                              
                                 // numberSet.add("");   
                              
                                // compilation error;   
                              
                                 print("Default implementation  :${numberSet.runtimeType}");    
                              
                                   
                              
                                 for(var i in numberSet) {   
                              
                                    print(i);   
                              
                                 }   
                              
                              } 

                                Output

                                10
                                20
                                30
                                40
                                50
                                

                                Example – Generics Queue

                                import 'dart:collection';   
                                
                                void main() {   
                                
                                   Queue<int> queue = new Queue<int>();   
                                
                                   print("Default implementation ${queue.runtimeType}");    
                                
                                   queue.addLast(100);   
                                
                                   queue.addLast(205);   
                                
                                   queue.addLast(315);   
                                
                                   queue.addLast(470);   
                                
                                   // Remove the first element of queue   
                                
                                   queue.removeFirst();    
                                
                                     
                                
                                   for(int i in queue){   
                                
                                      print(i);   
                                
                                   }   
                                
                                } 

                                  Output

                                  Default implementation ListQueue<int>
                                  205
                                  315
                                  470
                                  

                                  Generic Map

                                  As we know that declaring map require the key and value. The syntax is given below.

                                  Syntax:

                                  Map <Key_type, value_type>  

                                  Example –

                                  void main() {   
                                  
                                     Map <String, String>m={'name':'Joseph','Rollno':'Std1001'};   
                                  
                                     print('Map :${m}');   
                                  
                                  }

                                  Output

                                  Map :{name: Joseph, Rollno: Std1001}
                                  
                                1. Collection

                                  Dart doesn’t support the array to store the data, unlike the other programming language. We can use the Dart collection in place of array data structure. We can enable the other classes of the collection in our Dart script by using the dart::core library.

                                  Dart collection can be classified as follows.

                                  Dart CollectionDescription
                                  ListA list is the collection of the ordered group of collection. The dart::core library offers the list class that allows us to create and modify the list. It provides the following types of lists.Fixed Length List – We cannot change the list’s length at runtime.Growable List – We can change the length of the list at run-time.
                                  SetA set is the collection of the objects in which each object can be declared at once. The dart::core library offers the Set class to use its facilities.
                                  MapsThe maps are the collection of the key-value pair of data. Each value is stored with a particular key. The key and value can be any type in the dart. A Map is a dynamic collection. We can say that map can be modified at the run-time. The dart::core library makes available the Map class to work with it.
                                  QueueA queue is the collection of the where data stores in the first-in-first-out format. It can be manipulated at both ends. Simply, we can enter the element from one end and delete it from another end.

                                  Iterating Collections

                                  The dart::core library provides the iterator class, which enables the easy collection traversal. As we know that, every collection contains an iterator property. This property returns an iterator that point to the objects in the collection. Let’s understand the following example.

                                  Example –

                                  import 'dart:collection';   
                                  
                                  void main() {   
                                  
                                     Queue que = new Queue();   
                                  
                                     que.addAll([10,20,30]);    
                                  
                                     Iterator i= que.iterator;   
                                  
                                       
                                  
                                     while(i.moveNext()) {   
                                  
                                        print(i.current);   
                                  
                                     }   
                                  
                                  } 

                                    Output

                                    10
                                    20
                                    30
                                    

                                    Explanation:

                                    In the above code, the moveNext() function returned the Boolean value that indicating whether there is a subsequent entry. The current property of the returns the object of that iterator currently points to.

                                    HashMap <K, V Class>

                                    The HashMap class is based on the implementation of the Map. As we discussed earlier, the key must be unique and must have consistent Object == (equal to operator) and Object.hashCode implementations. We can also use null as a key. The elements in the Map may in any order. The iteration order only changes if the map is modified. If we iterate the map, the value of the map is iterated in the same order as their associated key.

                                  1. Typedef

                                    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.

                                    Declaring a typedef

                                    typedef 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.

                                    Syntax:

                                    typedef function_name(parameters)   

                                    Example –

                                    Let’s create an alias of MultiOperation(int n1, int n2) that contains two parameters of the integer type.

                                    typedef MultiOperation(int n1, int n2);   // function signature  

                                    Assigning typedef Variable

                                    We can assign any function with the same parameter to the typedef variable. The syntax is given below.

                                    Syntax:

                                    type_def var_name = function_name;  

                                    Let’s understand the following example, where we define the two functions with the same signature as the MultiOperation.

                                    Sum(int n1, int n2) {  
                                    
                                          print("Sum of the two number:${n1+n2}");  
                                    
                                    }  
                                    
                                    Sub(int n1, intn2 ) {  
                                    
                                          print("Subtraction of the two number:${n1-n2}");  
                                    
                                       
                                    
                                    }  

                                      Calling Function with typedef

                                      We can invoke function by passing the same parameter by using the typdef variable. The syntax is given below.

                                      Syntax:

                                      var_name(parameter);  

                                      Example:

                                      MultiOperation mp;  
                                      
                                      mp = Sum;  
                                      
                                      mp(20,10);  
                                      
                                      mp = Sub;  
                                      
                                      mp(30,20);

                                        The mp 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.

                                        Complete Program using typedef

                                        Let’s have a look at the following example.

                                        typedef MultiOperation(int num1, int num2);  // typedef function signature  
                                        
                                        Sum(int n1, int n2) {  
                                        
                                              print("Sum of the two number:${n1+n2}");  
                                        
                                        }  
                                        
                                        Sub(int n1, int n2 ) {  
                                        
                                              print("Subtraction of the two number:${n1-n2}");  
                                        
                                           
                                        
                                        }  
                                        
                                          
                                        
                                        void main() {  
                                        
                                        MultiOperation mp = Sum;  
                                        
                                        print("JavaTpoint - Dart typedef Example");  
                                        
                                          
                                        
                                        mp(20,10);  
                                        
                                        mp = Sub;  
                                        
                                        mp(30,20);  
                                        
                                        }

                                        Output:

                                        JavaTpoint - Dart typedef Example
                                        Sum of the two numbers: 30
                                        Subtraction of the two numbers: 10
                                        

                                        Explanation:

                                        In the above code, we created the alias of the MultiOperation() function using the typedef keyword. We defined two more functions Sum() and Sub(), which have same signature as the typedef function.

                                        Then, we assigned the typedef variable mp that referred to both functions Sum() function and Sub() function. Now, we invoked the function by passing the required argument, and it printed the result to the screen.

                                        Typedef as Parameter

                                        We can use the typedef method as a parameter. In the following example, we are creating an additional function to the above program NumericOperaion(int n1, int n2, MultiOperation mp) with the two integer variables and typedef ManyOperation mp as its parameter.

                                        Example –

                                        typedef MultiOperation(int num1, int num2);  // typedef function signature  
                                        
                                          
                                        
                                        Sum(int n1, int n2) {  
                                        
                                              print("Sum of the two number:${n1+n2}");  
                                        
                                        }  
                                        
                                        Sub(int n1, int n2 ) {  
                                        
                                              print("Subtraction of the two number:${n1-n2}");  
                                        
                                        }  
                                        
                                           
                                        
                                        NumericOperation(int n1, int n2, MultiOperation mp){  
                                        
                                              print("Inside Operation");  
                                        
                                              mp(n1,n2);  
                                        
                                                 }  
                                        
                                          
                                        
                                        void main() {  
                                        
                                        print("JavaTpoint - Dart typedef Example");  
                                        
                                        NumericOperation(20, 10, Sum);  
                                        
                                        NumericOperation(20, 10, Sub);  
                                        
                                        }  

                                          Output:

                                          JavaTpoint - Dart typedef Example
                                          Inside Operation
                                          Sum of the two number: 30
                                          Inside Operation
                                          Subtraction of the two number: 10
                                          

                                          In the above code, we didn’t need to create a typedef variable to the refer to each method; we just called the NumericOperation() function by passing the required value and typedef variable mp. It performed the given operations and printed the result.

                                          Dart Debugging

                                          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.

                                          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.

                                          What are Breakpoints?

                                          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.

                                          How to add Breakpoints in WebStorm?

                                          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.

                                          Dart Typedef
                                        1. Exceptions

                                          Dart Exceptions are the run-time error. It is raised when the program gets execution. The program doesn’t report the error at compile time when the program runs internally and if Dart compiler found something not appropriate. Then, it reports run-time error and the execution of program is terminated abnormally. This type of error is called Exceptions. For example – A given number is divided by the zero or we try to access the elements from the empty list.

                                          Dart supports the following types of built-in exceptions.

                                          Sr.ExceptionsDescription
                                          1.DefferedLoadExceptionIt is thrown when a deferred library fails to load.
                                          2.FromatExceptionIt is the exception which is thrown
                                          3.IntegerDivisionByZeroExceptionIt is thrown when number is divided by zero.
                                          4.IOEExceptionIt is the base class of input-output related exception.
                                          5.IsolateSpawnExceptionIt is thrown when an isolated cannot be created.
                                          6.TimeoutIt is thrown when a schedule timeout happens while waiting for an async result.

                                          The main objective of the exception is to handle the run-time error and prevent the program from terminating abruptly. Every exception in the Dart is a subtype of the pre-defined class Exception. Dart provides the following techniques to handle the exceptions.

                                          The try/on/catch Blocks

                                          The try block is used to hold the block of code that might be thrown an exception. The on block is used to when we require specifying the exceptions. The catch block is used to when handler needs the exception object.

                                          If the try block finds the error, it throws to the catch block and the catch block has the code to handle the error. The try block must be followed by the exactly one block either on/ catch or one finally block.

                                          The syntax of exceptional handling is the given below.

                                          Syntax:

                                          try {
                                          // code that might throw an exception
                                          }
                                          on Exception1 {
                                          // Specify the exception
                                          }
                                          Catch Exception2 {
                                          // code for handling exception
                                          }
                                          

                                          One should remember the following points.

                                          • We can handle the multiple exceptions using the more than one catch block.
                                          • The on block and the catch block is mutually inclusive that means we can associate the both – the on block and catch block with the try block.

                                          In the following example, the variable x is divided by the y variable respectively. The code is thrown when it tries to divide by the zero. The on block consists of the code to handle the exception. Let’s understand the following code.

                                          Example – Using the on block

                                          void main() {   
                                          
                                             int x = 12;   
                                          
                                             int y = 0;   
                                          
                                             int res;    
                                          
                                               
                                          
                                             try {  
                                          
                                                res = x ~/ y;   
                                          
                                             }   
                                          
                                             on IntegerDivisionByZeroException {   
                                          
                                                print('Cannot divide by zero');   
                                          
                                             }   
                                          
                                          }

                                          Output

                                          Cannot divide by zero
                                          

                                          Explanation:

                                          In the above code, we declared the three variable x, y and res in main () function. We written the suspect code in try block divided the x by the 0 that might be thrown an exception. The try block found the error the control transferred to the on block that has the code to handle the error. By using this, the program did not stop its execution.

                                          Let’s understand the following example using the catch block.

                                          Example – Using the catch Block

                                          void main() {   
                                          
                                             int x = 12;   
                                          
                                             int y = 0;   
                                          
                                             int res;    
                                          
                                               
                                          
                                             try {    
                                          
                                                res = x ~/ y;   
                                          
                                             }    
                                          
                                          // It returns the built-in exception related to the occurring exception  
                                          
                                             catch(E) {   
                                          
                                                print(E);   
                                          
                                             }   
                                          
                                          } 

                                          Output

                                          IntegerDivisionByZeroException
                                          

                                          Now look at the example of on…catch block together

                                          Example 3: on…catch block

                                          void main() {   
                                          
                                             int x = 12;   
                                          
                                             int y = 0;   
                                          
                                             int res;    
                                          
                                               
                                          
                                             try {   
                                          
                                                res = x ~/ y;   
                                          
                                             }    
                                          
                                             on IntegerDivisionByZeroException catch(E) {   
                                          
                                                print(E);   
                                          
                                             }   
                                          
                                          }  

                                          Output

                                          IntegerDivisionByZeroException
                                          

                                          The Finally Block

                                          The finally block always executes whether there is exception occur or not. It executes unconditionally after the try/on/catch.

                                          The syntax of finally block is given below.

                                          Syntax –

                                          try {   
                                          
                                             // code that may be throw an exception   
                                          
                                          }    
                                          
                                          on Exception1 {   
                                          
                                             // exception handling code or specifying the exception  
                                          
                                          }    
                                          
                                          catch Exception2 {   
                                          
                                             //  code for exception handling   
                                          
                                          }    
                                          
                                          finally {   
                                          
                                             // code that should always execute; whether exception or not.  
                                          
                                          } 

                                          Let’s understand the following example of finally block.

                                          Example –

                                          finally { void main() {   
                                          
                                             int x = 12;   
                                          
                                             int y = 0;   
                                          
                                             int res;    
                                          
                                               
                                          
                                             try {   
                                          
                                                res = x ~/ y;   
                                          
                                             }   
                                          
                                             on IntegerDivisionByZeroException {   
                                          
                                                print('Cannot divide by zero');   
                                          
                                             }   
                                          
                                            
                                          
                                                print('Finally block always executed');   
                                          
                                             }   
                                          
                                          }  

                                          Output

                                          Cannot divide by zero
                                          Finally block executed
                                          

                                          Throwing an Exception

                                          We can raise an exception explicitly or forcefully. The explicitly raised exception should be handled to avoid the program from existing sharply. The syntax is given below.

                                          Syntax:

                                          throw new Exception_name()  

                                          Let’s understand the following example.

                                          Example –

                                          main() {   
                                          
                                             try {   
                                          
                                                check_marks(-10);   
                                          
                                             }   
                                          
                                             catch(e) {   
                                          
                                                print('The marks cannot be negative');   
                                          
                                             }   
                                          
                                          }    
                                          
                                          void check_marks(int marks) {   
                                          
                                             if(marks<0) {   
                                          
                                                throw new FormatException();  // Raising explanation externally  
                                          
                                             }   
                                          
                                          } 

                                          Output

                                          The marks cannot be negative
                                          

                                          Custom Exceptions

                                          As we discussed above, each of the exception in dart is the subtype of the built-in class Exception. Dart provide the flexibility to create custom exception by extending the existing exception class. The syntax is given below.

                                          Syntax: Defining the Exception

                                          class Custom_exception_Name implements Exception {   
                                          
                                             // can contain constructors, variables and methods   
                                          
                                          }   

                                          Let’s understand the following code.

                                          Example –

                                          class AmtException implements Exception {   
                                          
                                             String expMsg() => 'Entered Amount should be greater than zero';   
                                          
                                          }    
                                          
                                          void main() {   
                                          
                                             try {   
                                          
                                                withdraw_amt(-1);   
                                          
                                             }   
                                          
                                             catch(E) {   
                                          
                                                print(E.expMsg());   
                                          
                                             }    
                                          
                                             finally {   
                                          
                                                print('Ending requested operation.....');   
                                          
                                             }   
                                          
                                          }    
                                          
                                          void withdraw_amt(int amt) {   
                                          
                                             if (amt <= 0) {   
                                          
                                                throw new AmtException();   
                                          
                                             }   
                                          
                                          }    

                                          Output

                                          Entered Amount should be greater than zero
                                          Ending requested operation.....
                                          

                                          Explanation:

                                          In the above example, we created a custom exception, AmtException. The code raised the exception if the entered amount is not within the excepted range and we enclosed the function invocation in the try…catch block.

                                        2. Interfaces

                                          An interface defines the syntax that any entity must adhere to. Dart does not have any separate syntax to define interfaces. An Interface defines the same as the class where any set of methods can be accessed by an object. The Class declaration can interface itself.

                                          The keyword implement is needed to be writing, followed by class name to be able to use the interface. Implementing class must provide a complete definition of all the functions of the implemented interface. We can say that a class must define every function with the body in the interface that we want to achieve.

                                          Declaring an Interface

                                          Dart doesn’t provide syntax for declaring interface directly. Implicitly, a class declaration itself an interface containing the entire instance member of the class and of any interfaces it implements.

                                          Implementing an Interface

                                          To work with interface methods, the interface must be implemented by another class using the implements keyword. A class which is implemented the interface must provide a full implementation of all the methods that belongs to the interface. Following is the syntax of the implementing interface.

                                          Syntax:

                                          class ClassName implements InterfaceName  

                                          In the following example, we are declaring a class Employee. Implicit, the Engineer class implements the interface declaration for the Employee class. Let’s understand the above example by the following code snippet.

                                          Example –

                                          class Employee  
                                          
                                          {  
                                          
                                             void display() {  
                                          
                                                   print("I am working as an engineer");  
                                          
                                                                      }  
                                          
                                          }  
                                          
                                          // Defining interface by implanting another class  
                                          
                                          class Engineer implements Employee   
                                          
                                          {  
                                          
                                                    void display() {  
                                          
                                                           print("I am an engineer in this company");                   
                                          
                                          }  
                                          
                                          }  
                                          
                                          void main()   
                                          
                                          {  
                                          
                                          Engineer eng = new Engineer();  
                                          
                                          eng.display();  
                                          
                                          } 

                                            Output:

                                            I am working as engineer
                                            

                                            Explanation

                                            In the above example, we defined a class Engineer as an interface implementing the Engineer class. Then, we defined the same method display() in both classes. This method override in class Engineer, so we created the object of the Engineer class in a main() function invoked the display() function. It printed the output to the screen.

                                            Implementing Multiple Inheritance

                                            We have discussed previously that the multiple inheritance is not supported by the Dart, but we can apply the multiple interfaces. We can say that, using multiple interfaces, we can achieve multiple inheritance in Dart. The syntax is given below.

                                            Syntax:

                                            class ClassName implements interface1, interface2,…interface n  

                                            Let’s understand the following example.

                                            Example –

                                            class Student  
                                            
                                            {  
                                            
                                               String name;  
                                            
                                               int age;  
                                            
                                                 
                                            
                                               void displayName() {  
                                            
                                                     print("I am ${name}");  
                                            
                                                                        }  
                                            
                                               void displayAge() {  
                                            
                                                        print("My age is ${age}");  
                                            
                                                                           }  
                                            
                                            }  
                                            
                                              
                                            
                                            class Faculty  
                                            
                                            {  
                                            
                                               String dep_name;  
                                            
                                               int salary;  
                                            
                                                 
                                            
                                               void displayDepartment() {  
                                            
                                                     print("I am a professor of ${dep_name}");  
                                            
                                                                        }  
                                            
                                               void displaySalary() {  
                                            
                                                        print("My salary is ${salary}");  
                                            
                                                                           }  
                                            
                                            }  
                                            
                                            // Defining interface by implenting another class  
                                            
                                            class College implements Student,Faculty  
                                            
                                            {    
                                            
                                               // Overriding the Student class members  
                                            
                                               String name;  
                                            
                                               int age;  
                                            
                                                 
                                            
                                               void displayName() {  
                                            
                                                     print("I am ${name}");  
                                            
                                                                        }  
                                            
                                               void displayAge() {  
                                            
                                                        print("My age is ${age}");  
                                            
                                                                           }  
                                            
                                              
                                            
                                            //Overriding each data member of Faculty class  
                                            
                                               String dep_name;  
                                            
                                               int salary;  
                                            
                                                 
                                            
                                               void displayDepartment() {  
                                            
                                                     print("I am a proffesor of ${dep_name}");  
                                            
                                                                        }  
                                            
                                               void displaySalary() {  
                                            
                                                        print("My salary is ${salary}");  
                                            
                                              
                                            
                                            }  
                                            
                                            }  
                                            
                                            void main()   
                                            
                                            {  
                                            
                                            College cg = new College();  
                                            
                                            cg.name = "Handscomb";  
                                            
                                            cg.age = 25;  
                                            
                                            cg.dep_name = "Data Structure";  
                                            
                                            cg.salary = 50000;  
                                            
                                              
                                            
                                            cg.displayName();  
                                            
                                            cg.displayAge();  
                                            
                                            cg.displayDepartment();  
                                            
                                            cg.displaySalary();  
                                            
                                            }

                                              Output:

                                              I am Handscomb
                                              My age is 25
                                              I am a professor of Data Structure
                                              My salary is 50000
                                              

                                              Explanation:

                                              In the above example, we implemented multiple interfaces in class College. Each data member of Student and Faculty class is overriding in class College. We created the object of College class and invoked the overriding functions. It printed the result.

                                              Rules for Implementing Interfaces

                                              1. A class that implements the interface must override every method and instance variable of an interface.
                                              2. Dart doesn’t provide syntax to declare the interface directly. The class declaration can consider as the interface itself.
                                              3. An interface class must provide the full implementation of all the methods belong to the interfaces.
                                              4. We can implement one or more interfaces simultaneously.
                                              5. Using the interface, we can achieve multiple inheritance.
                                            1. Abstract Classes

                                              Abstract classes are the classes in Dart that has one or more abstract method. Abstraction is a part of the data encapsulation where the actual internal working of the function hides from the users. They interact only with external functionality. We can declare the abstract class by using the abstract keyword. There is a possibility that an abstract class may or may not have abstract methods.

                                              Abstract methods are those methods, which are declared without implementation. The concrete methods or normal methods are declared with implementation. An abstract class can contain both types of methods, but a normal class is not allowed to have abstract methods.

                                              We cannot create the instance of an abstract class that means it can’t be instantiated. It can only be extended by the subclass, and the subclass must be provided the implantation to the abstract methods which are present in the present class. Then it is necessary to declare abstract subclass.

                                              Rules for Abstract classes:

                                              The rules of the abstract are given below.

                                              1. An abstract class can have an abstract method (method without implementation), or not.
                                              2. If there is at least one abstract method, then the class must be declared abstract.
                                              3. The object of the abstract class cannot be created, but it can be extended.
                                              4. An abstract keyword is used to declare the abstract class.
                                              5. An abstract class can also include normal or concrete (method with the body) methods.
                                              6. All abstract methods of parent class must be implemented in the subclass.

                                              Declaring Abstract Class

                                              An abstract keyword followed by a class name is used to declare the abstract class. An abstract class mostly used to offer a base for the subclass to extends and implement the abstract method.

                                              Syntax:

                                              abstract class ClassName {  
                                              
                                               // Body of abstract class  
                                              
                                              } 

                                                Usage of Abstract class

                                                Let’s suppose we have a class Person that has method displayInfo(), and we have to sub classes of it Boy and Girl. Each of the person information varies from the other person, so there is no benefit to implementing the displayInfo() in the parent class. Because every subclass must override the parent class method by provides its own implementation. Thus, we can force the subclass to provide implementation to that method, so that is the benefit to make method abstract. We don’t require the give implementation in the parent class.

                                                Dart Abstract Classes

                                                Let’s understand the above scenario through the following code.

                                                Example –

                                                abstract class Person {  
                                                
                                                //declaring abstract method  
                                                
                                                  
                                                
                                                void displayInfo();  //abstract method   
                                                
                                                  
                                                
                                                }  
                                                
                                                class Boy extends Person   
                                                
                                                {  
                                                
                                                // Overriding method  
                                                
                                                void displayInfo() {  
                                                
                                                    print("My name is Johnathon");  
                                                
                                                  
                                                
                                                       }  
                                                
                                                  
                                                
                                                }  
                                                
                                                  
                                                
                                                class Girl extends Person   
                                                
                                                {  
                                                
                                                // Overriding method  
                                                
                                                void displayInfo() {  
                                                
                                                    print("My name is Grecia");  
                                                
                                                  
                                                
                                                       }  
                                                
                                                  
                                                
                                                }  
                                                
                                                  
                                                
                                                void main() {  
                                                
                                                Boy b = new Boy();  // Creating Object of Boy class  
                                                
                                                Girl g = new Girl();  // Creating Object of Girl class  
                                                
                                                  
                                                
                                                b.displayInfo();  
                                                
                                                g.displayInfo();  
                                                
                                                }  

                                                  Output

                                                  My name is Johnathon
                                                  My name is Grecia
                                                  

                                                  Explanation:

                                                  As we can see that in the above code, we implemented the abstract method in two subclasses according to its requirement and then we called the displayInfo() method using the object of the both class’s object.

                                                1. Getters and Setters

                                                  Getters and setters are the special class method that is used to read and write access to an object’s properties. The getter method is used to reads the value of the variable or retrieve the value and setter method is used to set or initialize respective class fields. By default, all classes are associated with getter and setter method. However, we can override the default methods by defining getter and setter method explicitly.

                                                  Defining a getter

                                                  We can define the getters method by using the get keyword with no parameter a valid return type.

                                                  Syntax:

                                                  return_type get field_name{  
                                                  
                                                  } 

                                                    Defining a setter

                                                    We can declare the setter method using the set keyword with one parameter and without return type.

                                                    Syntax:

                                                    set field_name {  
                                                    
                                                    } 

                                                      Example:

                                                      class Student {  
                                                      
                                                                String stdName;  
                                                      
                                                                String branch;  
                                                      
                                                                int stdAge;  
                                                      
                                                       // getter method   
                                                      
                                                               String get std_name   
                                                      
                                                                   {  
                                                      
                                                                   return stdName;  
                                                      
                                                                   }  
                                                      
                                                               void set std_name(String name)  
                                                      
                                                                       {  
                                                      
                                                                    this.stdName = name;  
                                                      
                                                        
                                                      
                                                                     }  
                                                      
                                                                void set std_age(int age) {  
                                                      
                                                                     if(age> = 20){  
                                                      
                                                                         print("Student age should be greater than 20")  
                                                      
                                                                           }else{   
                                                      
                                                                               this.stdAge = age;  
                                                      
                                                                                    }  
                                                      
                                                                            }  
                                                      
                                                        
                                                      
                                                                                                               }  
                                                      
                                                                 int get std_age{  
                                                      
                                                                       return stdAge;  
                                                      
                                                        
                                                      
                                                      }  
                                                      
                                                              void set std_branch(String branch_name) {  
                                                      
                                                                       this.branch = branch_name;  
                                                      
                                                        
                                                      
                                                      }  
                                                      
                                                           int get std_branch{  
                                                      
                                                                  return branch;  
                                                      
                                                      }  
                                                      
                                                        
                                                      
                                                      }  
                                                      
                                                      void main(){  
                                                      
                                                      Student std = new Student();  
                                                      
                                                      std.std_name = 'John';  
                                                      
                                                      std.std_age = 24;  
                                                      
                                                      std.std_branch = 'Computer Science';  
                                                      
                                                        
                                                      
                                                      print("Student name is: ${std_name}");  
                                                      
                                                      print("Student age is: ${std_age}");  
                                                      
                                                      print("Student branch is: ${std_branch}");  
                                                      
                                                      }  

                                                        Output

                                                        Student name is: John
                                                        Student age is: 24
                                                        Student Branch is: Computer Science
                                                        

                                                        We can also place the getter and setter method just after the. Now, let’s understand the following example:

                                                        Example – 2

                                                        class Car {  
                                                        
                                                          String makedate;  
                                                        
                                                          String modelname;  
                                                        
                                                          int manufactureYear;  
                                                        
                                                          int carAge;  
                                                        
                                                          String color;  
                                                        
                                                        // Getter method  
                                                        
                                                          int get age {  
                                                        
                                                            return carAge;  
                                                        
                                                          }  
                                                        
                                                        // Setter Method  
                                                        
                                                          void set age(int currentYear) {  
                                                        
                                                            carAge = currentYear - manufactureYear;  
                                                        
                                                          }  
                                                        
                                                        // defining parameterized constructor  
                                                        
                                                          Car({this.makedate,this.modelname,this.manufactureYear,this.color,});  
                                                        
                                                        }  
                                                        
                                                        //Age here is both a getter and a setter. Let's see how we can use it.  
                                                        
                                                        void main() {  
                                                        
                                                         Car c =   
                                                        
                                                         Car(makedate:"Renault 20/03/2010",modelname:"Duster",manufactureYear:2010,color:"White");  
                                                        
                                                          print("The car company is: ${c.makedate}");   
                                                        
                                                          print("The modelname is: ${c.modelname}");   
                                                        
                                                          print("The color is:${c.color}");  
                                                        
                                                          c.age = 2020;  
                                                        
                                                          print(c.age);  
                                                        
                                                        } 

                                                          Output

                                                          The car company is: Honda 20/03/2010
                                                          The modelname is: City
                                                          The color is: White
                                                          10
                                                          

                                                          Explanation:

                                                          In the above code, we defined the getter and setter methods before the constructor.