Category Archives: Dart Tutorial

Dart Tutorial

Dart Typedef

Dart Typedef

In Dart, typedef is used to create an alias for function type that you can use as type annotations for declaring variables and return types of that function type. After creating an alias of function type; we can use it as type annotations in variable declaration or in function return type. A typedef holds type information when a function type is assigned to a variable.

Defining a typedef

A typedef keyword can be used to create an alias for function prototype that will comparable with the actual functions. A function prototype specifies function’s parameters (including their types).

Syntax:-

Example:-

Let’s create an alias ManyOperation for a function signature that will take two input parameters of the type integer.

Assigning typedef Variable

A typedef variable can be assigned any function having the same signature as typedef declaration.

Syntax:-

Example:-

Now, lets define some functions with the same function signature as that of the ManyOperation typedef.

Invoking Function with typedef

Now, the typedef variable can be used to invoke functions with same function signature.

Syntax:-

Example:-

The oper variable can be used to refer any method which takes two integer parameters. The typedefs can switch function references in runtime.

Complete Program In Action

Example:-

Output:-

dart_typedef_example

Typedefs as Parameter

Lets add one more method to the above program Calculator(int n1,int n2, ManyOperation oper) which accepts two integer numbers (n1 and n2) and one typedef ManyOperation oper as its parameter.

Example:-

Output:-

dart_typedef_as_function_parameters

Dart Debugging

Dart Debugging

Debugging is the process of detecting and removing of existing and potential errors in a Dart Program that can cause program to behave unexpectedly or crash. Debugging is used to find and fix bugs or defects so that program works as per the set specifications.

The WebStorm comes with rich debugging features. The debugger is one of the most essential features of WebStorm. With WebStorm you can develop, run, and debug Dart web and command-line applications. The WebStorm editor you are allowed to enable breakpoints for step-by-step debugging.

What Is Breakpoints?

Breakpoints are source code markers that let you break program execution at a specific point and examine its behavior. It prevent polluting our app with multiple print statements at checkpoints.

Adding a Breakpoint In WebStorm

In WebStorm, simply click on a line number in the left bar to attach a breakpoint. Once you have attached the breakpoints, simply run the program in debug mode. When program runs in debug mode, you will get the Debugger window where you can additionally configure how the breakpoint works. Additionally you can shows the values of variables in the current context. You can add watchers for specific variables and listen to that values changes using watches window.

Stepping Through Code

In WebStorm, the stepping actions are available on the top of the debug tool window, and they are activated when the breakpoint is hit.

Step Into (F7) :- This will execute code line by line in this or another file. But, if the current statement includes a function call, then execution will jump to its definition and stop at the first line.
Step over (F8) :- This will execute the code line by line in the current file.
Step Out (Shift-F8) :- This will finish the execution of the current function and then stop at the next statement after the call.

 

Dart Interfaces

Dart Interfaces

In Dart, we can think of an interface as a blueprint of a class; that any class entity must adhere to. Interfaces declares a set of methods available on an object. An interface can have methods and variables just like a class but in interface only abstract declaration of method is provided. Unlike classes, an interface can only contain method signatures, there is no full body implementation of the methods is provided. In order to use interface methods, the interface must be implemented by another class. Class should use the implements keyword (instead of extends) to be able to use an interface method. A class implementing interface must provide a concrete implementation of all the methods belongs to the interface. In other words, a class must redefine every method of the interface it is implementing.

Implicit interfaces

In Dart, every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. A class is allowed to implement one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces.

Usage of Interfaces

In Dart, interfaces is a way to achieve full abstraction. Since interface methods do not have body, the class that implements interface must implement all the methods of that interface before it can be accessed.

1.) An Interface is used to achieve abstraction.
2.) Interface is a mechanism to achieve multiple inheritance in Dart.

Declaring an Interface

In Dart, there is no way does for declaring interfaces directly. In Dart, class declarations themselves implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. .

Implementing an Interface

In order to use interface methods, the interface must be implemented by another class. Class should use the implements keyword (instead of extends) to be able to use an interface method. A class implementing interface must provide a concrete implementation of all the methods belongs to the interface. In other words, a class must redefine every method of the interface it is implementing.

Syntax:-

Example:-

Output:-

dart_interface_example

Implementing Multiple Interface

As we know the Dart does not support multiple inheritance, but a class can implement multiple interfaces. This way Interface can be used as a mechanism to achieve multiple inheritance in Dart.

Syntax:-

Example:-

Output:-

dart_implementing_multiple_interfaces_example

Rules For Implementing An Interfaces

1.) Any class implement an Interface must override every method and instance variable of an interface.
2.) In Dart, there is no syntax for declaring an interfaces, class declaration itself implicitly defines an interface.
3.) A class implementing interface must provide a concrete implementation of all the methods belongs to the interface.
4.) A class can implement more than one interface simultaneously.
5.) A class can extend only one class, but can implement multiple interfaces.

Dart Abstract classes

Dart Abstract Classes

Any class that contains one or more abstract methods is known as abstract class. A class can be declared abstract using the “abstract” keyword followed by class declaration. A class declared abstract may or may not include abstract methods. An abstract class can have abstract methods(methods without implementation) as well as concrete methods (methods with implementation). A normal class(non abstract class) is not allowed to have abstract methods.

An abstract class can not be instantiated, which means you are not allowed to create an object of it. Abstract classes can only be extended; and the subclass must provide implementations for all of the abstract methods in its parent class. If a subclass does not implements abstract methods, then the subclass must also be declared abstract.

Note :- An abstract method is a method that is declared without an implementation.

Rules for Abstract Classes

1.) Abstract classes may or may not include abstract methods (methods without body).
2.) If a class has at least one abstract method, then the class must be declared abstract.
3.) An abstract class can not be instantiated, but can be extended .
4.) If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
5.) An abstract class can be declared using abstract keyword.
6.) An abstract class may also have concrete (methods with body) methods.

Declaring Abstract Class

An abstract class can be defined using abstract keyword followed be class declaration. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods.

Syntax:-

Usage Of Abstract Class

Lets say we have a class Employee that has a method showEmpInfo() and we have two subclasses of it Manager and Engineer. Since the each of the employee information differs from other employee, then there is no point to implement showEmpInfo() method in parent class. This is because every subclass must override this method to give its own implementation. Thus, making this method abstract would be the good choice as by making this method abstract we force all the sub classes to implement this method, also we need not to give any implementation to this method in parent class.

Example:-

Now, when we run above Dart Program, we will see following output.

Output:-

dart_method_overriding_example

Dart Getters and Setters

Dart Getters and Setters

Getters and setters are special class methods that is used to initialize and retrieve the values of class fields respectively. The setter method is used to set or initialize respective class fields, while the getter method is used to retrieve respective class fields. All classes have default getter and setter method associated with it. However, you are free to override the default ones by implementing the getter and setter method explicitly.

Defining a getter

The getters are defined using the get keyword with no parameters and returns a value.

Syntax:-

Defining a setter

The setters are defined using the set keyword with one parameter and no return value.

Syntax:-

Example:-

The following example shows how you can use getters and setters in a Dart class –

Output:-

dart_getters_setters_example

Dart Method Overriding

Dart Method Overriding

Extending a super class allows its subclass to use methods defined in super class by simply creating objects of subclass. This way we are allowed to use inherited methods in subclass without having to define it again in subclass. However, there may be occasions when we want subclass objects to respond differently to the same method when it is invoked using subclass objects. This is possible by defining same method again in subclass with the same name, same arguments and same return type as in the same method in superclass. Now, when that method is called, the method defined in subclass is invoked and executed instead of the method defined in superclass.

Declaring a method in sub class which is already exists in its parent class is known as method overriding. In Method Overriding, the subclass provide its own implementation for the method which already exists in superclass. Here, method defined in super class is known as overridden method and the method in subclass is called overriding method. Now, call to the method from subclass object will always call the subclass version of the method. However, using super keyword you can call super method.

Example:-

Lets take a simple example to understand the concept of Method Overriding. We have two classes, a sub class SubClass and a super class SuperClass. The SubClass extends SuperClass. Both the classes have a common method void display() with different implementation. SubClass class is giving its own implementation to the display() method or in other words it is overriding the display() method.

When we run the above Dart Program, we will see the following output –

Output:-

dart_method_overriding_example

Advantage Of Method Overriding

The main advantage of method overriding is that the individual subclass can provide its own implementation to a inherited method as per the requirement; without even modifying the super class method. This is helpful when we want a subclass objects to respond differently to the same method when it is invoked using subclass objects.

Rules for Method Overriding

1.) A method can only be written in Subclass, not in same class.

2.) The argument list should be exactly the same as that of the overridden method.

3.) The return type should be the same as declared in the original overridden method in the super class.

4.) A method declared final cannot be overridden.

5.) A method declared static cannot be overridden.

6.) If a method cannot be inherited then it cannot be overridden.

7.) Constructors cannot be overridden.

Dart Methods

Dart Methods

A method is a set of statements that means to attach some behavior to class object. Method performs some action over class object, we give it a name so that it can be invoked later in the program. Method makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program. We pass information to methods as its parameter and method performs some action, after that it can either returns some value to the point it where it called from or returns nothing. Methods in a class can be either object method or class methods.

Instance Methods

Unless a method is declared static all of the methods in a class are instance methods. Instance methods are allowed to access instance variables and this.

Declaring Instance Methods

An instance method is defined by providing name of the method with list of parameters and return type if any.

Syntax:-

Calling Instance Method

Instance methods are applicable to objects, thus you required to create an instance of a class to invoke it.

Syntax:-

Class Methods

All of the methods declared with static keyword are class methods. Static method belong to class instead of class instances. A static method is only allowed to access the static variables of class and can invoke only static methods of the class.

Declaring Class Methods

A class method can be declared using static keyword followed by return type, followed by method name.

Syntax:-

Calling Class Method

Class methods can be invoked directly from the class name itself rather than creating an instance of it.

Syntax:-

Dart Super Constructor

Dart Super Constructor

A subclass inherits the variables and methods from its superclass, but the superclass constructor is not inherited in the subclass. The superclass constructors can only be invoked from subclass constructors using the super() constructor. The super() constructor allows a subclass constructor to explicitly call the no-arg and parameterized constructor of superclass.

Syntax:-

Implicit super:-

When an object of subclass is created using the new keyword, it invokes the subclass constructor which implicitly invokes the parent class’s default (no-arg constructor) constructor. If no parameters are defined in a superclass constructor, you can bypass the call to super() in your subclass.

Example:-

Output:-

dart_implicit_super_constructor_keyword_example

Explicit super:-

If the constructor in superclass takes arguments then we are required to use parameterized super() method in subclass constructor to invoke parameterized constructor of superclass and pass the requested arguments.

Example 1:-

Output 1:-

dart_parameterized _super_constructor_keyword_example

Example 2:-

Output:-

dart_super_constructor_example

Dart super Keyword

Dart super Keyword

The super keyword is a reference variable which is used to refer immediate parent class object. It is used to refer the superclass properties and methods. This is possible because when we create an instance of subclass, an instance of its parent class is created implicitly which we can refer using the super keyword. The most common use of the super keyword is to eliminate the ambiguity between superclasses and subclasses that have variables and methods with the same name.

Usage of super Keyword

1) The super keyword can be used to access the data members of parent class when both parent and child class have member with same name.
2) The super keyword can be used to access the method of parent class when child class has overridden that method. 3) The super keyword can be used to explicitly call the no-arg and parameterized constructor of parent class

Use super keyword to access parent class variables

When you have a variable in sub class which is already exists in its super class; then the super keyword can be used to access variable in super class.

Syntax:-

Example:-

In the following program, we have a variable num declared in the SubClass, the variable with the same name is already present in the SuperClass.

Output:-

Here, if we use print(num); instead of print(super.num); it will print 100 instead of 50.

Use super keyword to invoke parent class method

When a subclass contains a method already present in the superclass then it is called method overriding. In method overriding call to the method from subclass object will always invoke the subclass version of the method. However, using the super keyword we are allowed to invoke superclass version of the method.

Syntax:-

Example:-

Output:-

Note:-When subclass doesn’t override the superclass method then there is no need to use the super keyword to call the superclass method.

Dart static Keyword

Dart static Keyword

The static keyword is used for memory management of global data members. The static keyword can be applied to the fields and methods of a class. The static variables and methods are part of the class instead of a specific instance. The static keyword is used for a class level variable and method that is the same for every instance of a class; this means if you make a data member static, you can access it without creating a object. The static keyword allows data members to persist values between different instances of a class. There is no need to create a class object to access a static variable or call a static method; simply put the class name before the static variable or method name to use them.

Dart Static Variables

The static variables belongs to the class instead of a specific instance. A static variable is common to all instances of a class; this means only a single copy of static variable is shared among all the instances of a class. The memory allocation for static variable happens only once in class area at the time of class loading.

Important Points :-

  • Static variables are also known as Class Variables.
  • Static variables can be accessed directly in Static method
  • Single copy of static variable is shared among all the instances of a class

Declaring Static Variables

Static variables can be declared using the static keyword followed by data type then variable name.

Syntax:-

Accessing Static Variable

Static variable can be accessed directly from the class name itself rather than creating an instance of it.

Syntax:-

Dart Static Methods

Same as static variable, static method belong to class instead of class instances. A static method is only allowed to access the static variables of class and can invoke only static methods of the class. Usually, utility methods are created as static methods when we want it to be used by other classes without the need of creating an instance.

Important Points :-

  • Static methods are also known as Class Methods.
  • Static method is only allowed to access the static variables of class.
  • Single copy of static method is shared among all the instances of a class
  • Static methods can be accessed directly using class name.

Declaring Static Methods

Static method can be declared using static keyword followed by return type, followed by method name.

Syntax:-

Calling Static Method

Static methods can be invoked directly from the class name itself rather than creating an instance of it.

Syntax:-

Example:-

Output:-

dart_static_keyword_example

Dart this Keyword

Dart this Keyword

The this keyword represents an implicit object pointing to current class object. It refers to the current instance of the class in a method or constructor. The this keyword is mainly used to eliminate the ambiguity between class attributes and parameters with the same name. When the class attributes and the parameter name are same; this keyword is used to avoid ambiguity by prefixing class attributes with the this keyword. The this keyword can be used to refer to any member of the current object from within an instance method or a constructor.

Uses Of this Keyword

  • It can be used to refer instance variable of current class
  • It can be used to invoke or initiate current class constructor
  • It can be passed as an argument in the method call
  • It can be passed as argument in the constructor call
  • It can be used to invoke current class method
  • It can be used to return the current class instance

Let’s take following example to see how to use the this keyword in Dart.

Example:-

When you run above Dart program, you will see following output –

dart_this_keyword_example