0% found this document useful (0 votes)
8 views12 pages

Lecture 4

Uploaded by

shahida.jasmine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Lecture 4

Uploaded by

shahida.jasmine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Dart Language Part-2

IT Industry-Academia Bridge Program


Decision making and Loops
• A decision-making block evaluates a condition before the instructions are executed.
Dart supports If, If.else, and switch statements.
• Loops are used to repeat a block of code until a specific condition is met. Dart supports
for, for.in, while and do.while loops
• Let us understand a simple example of the usage of control statements and loops:
for( var i = 1 ; i <= 10; i++ ) {
if(i%2==0)
{
print(i);
}

IT Industry-Academia Bridge Program


Ternary Operator in Dart and Flutter
• Syntax
• condition ? exprIfTrue : exprIfFalse

• Example
String message(bool isValid) {
return isValid ? 'This is valid' : 'This is not valid';
}

var ans = 10;


ans == 1 ? print("Answer is 10") : print("Oh no!");
IT Industry-Academia Bridge Program
Functions
• A function is a group of statements that together performs a specific task. Let us look into a
simple function in Dart as shown here:
void main() {
add(3,4);
}
void add(int a,int b) {
int c;
c=a+b;
print(c);
}

The above fu.nction adds two values and produces 7 as the output
IT Industry-Academia Bridge Program
Object Oriented Programming
• Dart is an object-oriented language. It supports object-oriented programming features
like Classes, Objects, Inheritance, Polymorphism, Interfaces, and Abstract classes.

• A class is a blueprint for creating objects. A class definition includes the following:
∙ Fields,
∙ Getters and setters
∙ Constructors
∙ Functions

IT Industry-Academia Bridge Program


Cont.….

Now, let us create a simple class using the above definitions:
• class Employee
{
String name;
String get emp_name //getter method
{ return name;
}

void set emp_name(String name) //setter method


{ this.name = name;
}

void result() //function definition


{ print(name);
}
}
void main() {
Employee emp = new Employee();
emp.name="employee1";
emp.result();
IT Industry-Academia Bridge Program
}
Cont…
An object is a real-life entity such as a table, human, car, etc. The object has two
characteristics - state and behavior. Let's take an example of a car which has a name, model
name, price and behavior moving, stopping, etc. The object-oriented programming offers to
identify the state and behavior of the object.
We can access the class properties by creating an object of that class. In Dart, The object can
be created by using a new keyword followed by class name. The syntax is given below.
var objectName = new ClassName(<constructor_arguments>)
• Inheritance
Dart supports inheritance, which is used to create new classes from an existing class.
Dart provides extends keyword to inherit the properties of parent class in child class. The
syntax is given below.
class child_class_name extends parent_class_name
IT Industry-Academia Bridge Program
Object-Oriented Programming
• Interfaces
The interface is defined as a blueprint of the class. We can declare methods and variables inside
the interface just like the class but in the interface, only an abstract declaration of methods is
provided. We can only define the function signature but not its body. Another class can
implement the interface. It is basically used for data-hiding.
Dart does not have a syntax for declaring interfaces. Class declarations are themselves interfaces
in Dart.
Classes should use the implements keyword to be able to use an interface.
It is mandatory for the implementing class to provide a concrete implementation of all the
functions of the implemented interface. In other words, a class must redefine every function in
the interface it wishes to implement.
Syntax: class identifier implements interface_name.
IT Industry-Academia Bridge Program
Example..
void main() {
ConsolePrinter cp= new ConsolePrinter();
cp.print_data();
}
class Printer {
void print_data() {
print("__________Printing Data__________");
}
}
class ConsolePrinter implements Printer {
void print_data() {
print("__________Printing to Console__________");
}
}

IT Industry-Academia Bridge Program


void main() {
Calculator c = new Calculator();
print("The gross total : ${c.ret_tot()}");
print("Discount :${c.ret_dis()}");
}
class Calculate_Total {
void ret_tot() {}
}
class Calculate_Discount {
void ret_dis() {}
}
class Calculator implements Calculate_Total,Calculate_Discount {
int ret_tot() {
return 1000;
}
int ret_dis() {
return 50;
}
}
IT Industry-Academia Bridge Program
Object Oriented Programming
• Abstract Class
A class that contains one or more abstract methods is called an abstract
class. We can declare the abstract class using the abstract keyword
followed by class declaration. The syntax is given below.
abstract class ClassName {
//
}

IT Industry-Academia Bridge Program


Enums In Dart and Flutter
• In Dart, Enums are a special kind of class used to represent a fixed number of constant values
enum Result {
LoginSuccess,
LoginFail}
void main(){
var result = Result.LoginSuccess;
switch(result){
case Result.LoginSuccess:
print("Login successfully");
break;
case Result.LoginFail:
print("Mobile or email invalid");
break;}}
IT Industry-Academia Bridge Program

You might also like