GDG Week 2 - Advanced Dart
GDG Week 2 - Advanced Dart
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 .
Output:
Without WithConstructo
Constructor
Person person = Person(); r
Person person = Person( “ John “ , 30 ) ;
person.name = “John”;
Person.age = 30;
• 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.
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
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");
}
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
• 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.
HANDLING ERRORS
You can handle errors in the dart
async function by using try-catch .
29
Task