1 of 55

Dart Concepts

DSC SVIT�DSC PU

arps18

2 of 55

Topics

  • Inheritance
  • Getters & Setters
  • Abstract Class & Methods
  • Generics
  • Safety Operators
  • Async

DSC SVIT�DSC PU

arps18

3 of 55

DSC SVIT�DSC PU

arps18

4 of 55

Comments

  • Dart supports single-line comments, multi-line comments, and documentation comments.
  • Single-line comments
    • A single-line comment begins with //. Everything between // and the end of line is ignored by the Dart compiler.

DSC SVIT�DSC PU

arps18

5 of 55

Comments

  • Multi-line comments
    • A multi-line comment begins with /* and ends with */. Everything between /* and */ is ignored by the Dart compiler (unless the comment is a documentation comment; see the next section).

DSC SVIT�DSC PU

arps18

6 of 55

DSC SVIT�DSC PU

arps18

7 of 55

Getters and Setters

  • Getters and Setters, also called as accessors and mutators, allow the program to initialize and retrieve the values of class fields respectively.
  • Getters or accessors are defined using the get keyword.
  • Setters or mutators are defined using the set keyword.

DSC SVIT�DSC PU

arps18

8 of 55

Getters and Setters

Getters and Setters

Getters and Setters

Getters & Setters

  • Syntax: Defining a getter

Syntax: Defining a setter

DSC SVIT�DSC PU

arps18

9 of 55

Sample Code

DSC SVIT�DSC PU

arps18

10 of 55

Inheritance

DSC SVIT�DSC PU

arps18

11 of 55

DSC SVIT�DSC PU

arps18

12 of 55

DSC SVIT�DSC PU

arps18

13 of 55

DSC SVIT�DSC PU

arps18

14 of 55

Class Inheritance

  • A class inherits from another class using the ‘extends’ keyword.
  • Child classes inherit all properties and methods except constructors from the parent class.

Syntax

DSC SVIT�DSC PU

arps18

15 of 55

Types of Inheritance

Inheritance can be of the following three types −

  • Single − Every class can at the most extend from one parent class.
  • Multiple − A class can inherit from multiple classes. Dart doesn’t support multiple inheritance.
  • Multi-level − A class can inherit from another child class.

DSC SVIT�DSC PU

arps18

16 of 55

DSC SVIT�DSC PU

arps18

17 of 55

Sample Code

DSC SVIT�DSC PU

arps18

18 of 55

DSC SVIT�DSC PU

arps18

19 of 55

SAMPLE CODE

DSC SVIT�DSC PU

arps18

20 of 55

Abstract Methods

  • Abstract methods can only exist in abstract classes.
  • To make a method abstract ,use semicolon ( ; ) instead of method body
  • Abstract Method can only exist with Abstract class

DSC SVIT�DSC PU

arps18

21 of 55

Abstract Classes

  • Use abstract keyword to declare abstract Class.
  • Abstract class can have Abstract Methods,normal Methods & Instance Variables as well.

DSC SVIT�DSC PU

arps18

22 of 55

Syntax for defining Abstract Class

abstract class class_name

{

//Body of Abstract Class

}

DSC SVIT�DSC PU

arps18

23 of 55

Enumerated Types

  • Enumerated types, often called enumerations or enums, are a special kind of class used to represent a fixed number of constant values.
  • By default, the value of the first enumeration symbol is 0.
  • Declare an enumerated type using the enum keyword:

DSC SVIT�DSC PU

arps18

24 of 55

DSC SVIT�DSC PU

arps18

25 of 55

Generics

  • Generics are often required for type safety.
  • The <…> notation marks List as a generic (or parameterized) type—a type that has formal type parameters.
  • The type is List<E>

DSC SVIT�DSC PU

arps18

26 of 55

Sample Code

DSC SVIT�DSC PU

arps18

27 of 55

Typedef

  • A typedef, or a function-type alias, helps to define pointers to executable code within memory.
  • Simply put, a typedef can be used as a pointer that references a function.

DSC SVIT�DSC PU

arps18

28 of 55

DSC SVIT�DSC PU

arps18

29 of 55

DSC SVIT�DSC PU

arps18

30 of 55

Safety Operators

  • Collectively known as null-aware operators, these new features will help you reduce the code required to work with potentially null objects.

??

Use ?? when you want to evaluate and return an expression IFF another expression resolves to null.

exp ?? otherExp

DSC SVIT�DSC PU

arps18

31 of 55

DSC SVIT�DSC PU

arps18

32 of 55

Safety Operators

??=

Use ??= when you want to assign a value to an object IFF that object is null. Otherwise, return the object.

obj ??= value

DSC SVIT�DSC PU

arps18

33 of 55

DSC SVIT�DSC PU

arps18

34 of 55

Safety Operators

?.

Use ?. when you want to call a method/getter on an object IFF that object is not null (otherwise, return null).

obj?.method()

DSC SVIT�DSC PU

arps18

35 of 55

DSC SVIT�DSC PU

arps18

36 of 55

DSC SVIT�DSC PU

arps18

37 of 55

DSC SVIT�DSC PU

arps18

38 of 55

Static Keyword

  • The Static keyword can be applied to the data members of the class ,i.e fields & methods.

DSC SVIT�DSC PU

arps18

39 of 55

Async

  • An asynchronous operation executes in a thread, separate from the main application thread.
  • When an application calls a method to perform an operation asynchronously, the application can continue executing while the asynchronous method performs its task.

DSC SVIT�DSC PU

arps18

40 of 55

Create a contact.txt file as given below and save it in the current project.

DSC SVIT�DSC PU

arps18

41 of 55

DSC SVIT�DSC PU

arps18

42 of 55

Future

  • The Dart community defines a Future as "a means for getting a value sometime in the future." Simply put, Future.
  • Dart is a single-threaded programming language. If any code blocks the thread of execution , the program effectively freezes.
  • Asynchronous operations let your program run without getting blocked. Dart uses Future objects to represent asynchronous operations.

DSC SVIT�DSC PU

arps18

43 of 55

DSC SVIT�DSC PU

arps18

44 of 55

Classes

A class encapsulates data for the object.

Generics

Generics are often required for type safety, Properly specifying generic types results in better generated code & You can use generics to reduce code duplication.

Safety Operators

Collectively known as null-aware operators, these features will help to reduce the code required to work with potentially null objects.

Typedefs

A typedef, or a function-type alias, helps to define pointers to executable code within memory.

Asynchrony support

An asynchronous operation executes in a thread, separate from the main application thread.

DSC SVIT�DSC PU

arps18

45 of 55

Classes

A class encapsulates data for the object.

Generics

Generics are often required for type safety, Properly specifying generic types results in better generated code & You can use generics to reduce code duplication.

Safety Operators

Collectively known as null-aware operators, these features will help to reduce the code required to work with potentially null objects.

Typedefs

A typedef, or a function-type alias, helps to define pointers to executable code within memory.

Asynchrony support

An asynchronous operation executes in a thread, separate from the main application thread.

DSC SVIT�DSC PU

arps18

46 of 55

Classes

A class encapsulates data for the object.

Generics

Generics are often required for type safety, Properly specifying generic types results in better generated code & You can use generics to reduce code duplication.

Safety Operators

Collectively known as null-aware operators, these features will help to reduce the code required to work with potentially null objects.

Typedefs

A typedef, or a function-type alias, helps to define pointers to executable code within memory.

Asynchrony support

An asynchronous operation executes in a thread, separate from the main application thread.

DSC SVIT�DSC PU

arps18

47 of 55

Classes

A class encapsulates data for the object.

Generics

Generics are often required for type safety, Properly specifying generic types results in better generated code & You can use generics to reduce code duplication.

Safety Operators

Collectively known as null-aware operators, these features will help to reduce the code required to work with potentially null objects.

Typedefs

A typedef, or a function-type alias, helps to define pointers to executable code within memory.

Asynchrony support

An asynchronous operation executes in a thread, separate from the main application thread.

DSC SVIT�DSC PU

arps18

48 of 55

Classes

A class encapsulates data for the object.

Generics

Generics are often required for type safety, Properly specifying generic types results in better generated code & You can use generics to reduce code duplication.

Safety Operators

Collectively known as null-aware operators, these features will help to reduce the code required to work with potentially null objects.

Typedefs

A typedef, or a function-type alias, helps to define pointers to executable code within memory.

Asynchrony support

An asynchronous operation executes in a thread, separate from the main application thread.

DSC SVIT�DSC PU

arps18

49 of 55

Comments

Dart supports single-line comments, multi-line comments, and documentation comments.

Enum

Enums are a special kind of class used to represent a fixed number of constant values.

Getters & Setters

allow the program to initialize and retrieve the values of class fields respectively.

Inheritance

Inheritance means that a class can directly inherit from one class to another.

DSC SVIT�DSC PU

arps18

50 of 55

Comments

Dart supports single-line comments, multi-line comments, and documentation comments.

Enum

Enums are a special kind of class used to represent a fixed number of constant values.

Getters & Setters

allow the program to initialize and retrieve the values of class fields respectively.

Inheritance

Inheritance means that a class can directly inherit from one class to another.

DSC SVIT�DSC PU

arps18

51 of 55

Comments

Dart supports single-line comments, multi-line comments, and documentation comments.

Enum

Enums are a special kind of class used to represent a fixed number of constant values.

Getters & Setters

allow the program to initialize and retrieve the values of class fields respectively.

Inheritance

Inheritance means that a class can directly inherit from one class to another.

DSC SVIT�DSC PU

arps18

52 of 55

Comments

Dart supports single-line comments, multi-line comments, and documentation comments.

Enum

Enums are a special kind of class used to represent a fixed number of constant values.

Getters & Setters

allow the program to initialize and retrieve the values of class fields respectively.

Inheritance

Inheritance means that a class can directly inherit from one class to another.

DSC SVIT�DSC PU

arps18

53 of 55

DSC SVIT�DSC PU

arps18

54 of 55

DSC SVIT�DSC PU

arps18

55 of 55

DSC SVIT�DSC PU

arps18