0% found this document useful (0 votes)
61 views34 pages

GDG Week 2 - Advanced Dart

a Flutter document on enhancing Flutter app reserch

Uploaded by

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

GDG Week 2 - Advanced Dart

a Flutter document on enhancing Flutter app reserch

Uploaded by

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

ADVANCED DART

Google Developers Group on Campus


Addis Ababa Science and Technology University
2

CONTENTS
o Object-Oriented Programming o Asynchronous Programming
o Class and objects o Async and Await
o Future
o Constructor
o Exception Handling
o Encapsulation
o Try Catch
o Inheritance

o Polymorphism

GDSC
OOP IN DART
DART –CLASSES AND OBJECTS
• Dart is an object-oriented programming language, so it supports the
concept of class, object …etc.
• In Dart, we can define classes and objects of our own .
• We use the class keyword to do so.
• Dart is support object-oriented programming features like classes and
interfaces.
5

• Class is the blueprint of objects and class is the collection of data members and data function
means which include these fields, getter and setter, and constructor and functions.

• Objects are the instance of the class and they are declared by using new keyword followed by the
class name .

Declaring class in Dart : Declaring object in Dart:


Syntax: Syntax:
class class_name { Var object_name = new class_name(
// body of class [arguments] );
}
• new is the keyword use to declare the instance of the class
• Class is the keyword use to initialize the • Object_name is the name of the object its naming is similar
class. to the variable name in dart
• Class_name is the name of the class. • class_name is the name of the class whose instance variable
• Body of class consists of fields, is been created.
constructors, getter and setter methods, • Arguments are the input which are needed to be pass if we
etc. are willing to call a constructor.
6

• After the object is created to access the fields we


use ( . ) operator for that purpose.

Output:

Welcome to Addis Ababa


CONSTRUCTOR IN DART
• A constructor is a special method used to initialize an object
• It is called automatically when an object is created and it can be used to set the
initial values for the object’s properties
8

• The constructor’s name should be the same as the class name


• Constructor doesn’t have any return type.
• If you don’t define a constructor for class, then you need to set the value of the properties
manually

Without WithConstructo
Constructor
Person person = Person(); r
Person person = Person( “ John “ , 30 ) ;
person.name = “John”;
Person.age = 30;

Syntac for declaration


Constructor
Class ClassName {
// Constructor declaration : same as class name
ClassName() {
//body of the constructor
}
}
9

Example of Constructor Example of Single line Constructor


ENCAPSULATION IN DART
• Encapsulation is one of the important concepts of oop.
• Encapsulation means hiding data within a library, preventing it from
outside factors.
• It helps you control your program and prevent it from becoming too
complicated.
11

• To Achieve Encapsulation we need to Declare the class properties as private by using underscore ( _
).

• Providing public getter and setter methods to access and update the value of private property.

• Getter and setter methods are used to access and update the value of private property.

• Getter methods are used to access the value of private property .

• Setter methods are used to update the value of private property


12
CONCEPT OF INHERITANCE
In Dart one class can inherit another class i.e dart can create a new class from an
existing class.
We make use of extend keyword to inherit another class
Parent Class – it is the class whose properties are inherited by the child class.
Child Class – it is the class that inherits the properties of the other classes.
Output:

This is This is class A

• Child classes inherit all properties and methods except


constructors of the patent class.
• Like Java, Dart also doesn’t support multiple inheritance
15

What is Super in Dart


• Super is used to refer to the parent class.
• It is used to call the parent class’s properties
and methods.

Output
:
MacBookPro display
MacBook display
Laptop display
POLYMORPHISM IN DART
• Poly means many and morph means forms.
• Polymorphism is the ability of an object to take on many forms.
• As humans, we have the ability to take on many forms. We can be a
student, a teacher, a parent, a friend, and so on. Similarly, in
object-oriented programming, polymorphism is the ability of an object to
take on many forms.
17

Output

It runs on petrol.
It runs on electricity.
18

Task

1. Write a dart program to create a class Laptop with properties [ id , name , ram ]
and create 3 objects of it and print all details.

1. Write a dart program to create a class Animal with properties [ id, name , color ].
Create another class called Cat and extends it from Animal. Add new properties
sound in String . Create an object of a Cat and print all details.
ASYNCHRONOUS PROGRAMMING
ASYNCHRONOUS PROGRAMMING
IN DART
• Asynchronous Programming is a way of writing code that allows a program to do
multiple task at the same time.
• Time consuming operations like fetching data from the internet, writing to a
database, reading from a file, and downloading a file can be performed without
blocking the main thread of execution
21

Synchronous Programming
• In Synchronous programming the program is executed line by line, one at a time.
• Synchronous operation means a task that needs to be solved before proceeding to the next
one

void main() { • Let’s assume Second Operation takes 5 seconds to


print("First Operation"); load then Third Operation and Last Operation need to
print("Second Operation"); wait for 3 seconds. To solve this issue you can use
print("Third Operation"); asynchronous programming.
print("Last Operation");
}
22

Asynchronous Programming
• In Asynchronous programming the program execution continues to the next line without waiting to
complete other work.
• It simply means, Don’t Wait
• It represents the task that doesn’t need to solve before proceeding to the next one.

void main() {
print("First Operation");
Future.delayed(Duration(seconds:5),()=>print('Second Operation'));
print("Third Operation");
print("Last Operation");
}

Output First Operation


Third Operation
Last Operation
Second Operation
23

WHY WE NEED ASYNCHRONOUS ?

• To Fetch Data From internet,

• To write something to database,

• To execute a long-time consuming


task,

• To read data from file,

• To download file etc.


24

FUTURE IN DART
• In dart, the Future represents a value or error that is not yet available,
• It is used to represent a potential value, or error, that will be available at some time
in the future
• You can create a future in dart by using Future class.
25

How to use Future in dart

you can use future in dart by using then()


method
26

ASYNC AND AWAIT IN DART

• Async/await is a feature in dart that allows us to write asynchronous code that looks and behaves like
synchronous code, making it easier to read
• When a function is marked async, it signifies that it will carry out some work that could take some time
and will return a future object that wraps the result of that work
• The await keyword allows you to delay the execution of an async function until awaited Future has
finished. This enables us to create code that appears to be synchronous but is actually asynchronous.

To define an Asynchronous function, add async before the function body


The await keyword work only in the async function.
27
28

HANDLING ERRORS
You can handle errors in the dart
async function by using try-catch .
29

Task

1. Write a program to print current time after 5 seconds using


Future.delayed()

1. Write a program in dart that uses Future class to perform multiple


asynchronous operations, wait for all of them to complete, and
then print the results.
30

EXCEPTION HANDLING IN DART


• An exception is an error that occurs at runtime during program execution.
• When the exception occurs, the flow of the program is interrupted, and
the program terminates abnormally. There is a high chance of crashing or
terminating the program when an exception occurs. Therefore, to save
your program from crashing, you need to catch the exception.
TRY & CATCH IN
DART
• Try – you can write the logical
code that creates exceptions in
the try block.
• Catch – when you are
uncertain about what kind of
exception a program
produces, then a catch block is
used. It is written with a try
block to catch the general
exception
FINALLY IN DART TRY
CATCH
• The finally block is always
executed whether the
exceptions occur or not.
• It’s optional to include the final
block, but if it is included, it
should be after the try and
catch block is over.
THROWING AN
EXCEPTION
• The throw keyword is used to
raise an exception explicitly
• A raised exception should be
handled to prevent the
program from exiting
unexpectedly .
THANK YOU

Google Developers Group on Campus


Addis Ababa Science and Technology University

You might also like