0% found this document useful (0 votes)
338 views

Chapter 3 Advanced Concepts in Dart

This document provides an overview of advanced concepts in Dart mobile application development, including null safety, classes, inheritance, abstract classes, enums, and asynchronous programming. It discusses how null safety prevents null value errors, and describes Dart classes, constructors, inheritance, abstract classes, enums, late variables, object-oriented programming principles, and null-aware operators for handling potential null values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
338 views

Chapter 3 Advanced Concepts in Dart

This document provides an overview of advanced concepts in Dart mobile application development, including null safety, classes, inheritance, abstract classes, enums, and asynchronous programming. It discusses how null safety prevents null value errors, and describes Dart classes, constructors, inheritance, abstract classes, enums, late variables, object-oriented programming principles, and null-aware operators for handling potential null values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Mobile

Application
Development
Advanced
Concept in Dart

Chapter Three
● NULL SAFETY IN DART
Outline ● Dart classes and constructors
● Class inheritance
● abstract classes
● The enum type
● The cascade notation
● Understanding Dart libraries and packages
● Introducing async programming
NULL SAFETY IN DARTt
● In simple words null safety in dart means variables cannot contain null value by default.
● This reduces the risk of runtime bugs and makes your code error-free.
● This feature is called Sound Null Safety in dart.
● A null safety feature is added to Dart 2.12 version.
int age = null; // give error
● Note: A value of type ‘Null’ can’t be assigned to a variable of type ‘int’.
● Programmers do have a lot of difficulties while handling null values.
● They forget that there are null values, so the program breaks.
● Here null mostly acts as time bomb for programmers, which is ready to break the program.
NULL SAFETY IN DARTt
● Non-Nullable By Default → Variables cannot be null by default. If you attempt to assign a null value, it
will give a compile-time error.
int productid = 20; // non-nullable
int productid = null; // give error
● How To Define Null Value → As you know with dart sound null Safety you cannot provide a null value
by default. If you are 100% sure to use it then you can use ? to the type declaration.
int ? productid = null; // Works
int? age;
HANDLE NULL VALUES
● How To Allow Null Values → If you want to allow null, you can add the question mark ? to the end of the
variable’s type.
int? productid = null;
Null Aware Operators In Dart
● If null operator ??
● Null aware assignment operator ??=
● Null aware access operator ?.
● Null assertion operator !
● Null aware method invocation operator ?.
Null Aware Operators In Dart
● If null operator ?? →One of the best ways to handle null values is to use a double question mark ??,
which is called the if null operator. This operator says if the value on the left is null go with the value on
the right, if a value is not null do nothing.
String? Address;
String status = address ?? "No Address";
● Null-Aware Assignment Operator ??= → Null-aware assignment operator in dart says if
the value is null then it assigns value after ??=, else don’t assign any other value.
String? address;
address ??= "USA";
Null Aware Operators In Dart
● Null-Aware Access Operator ?. →The null-aware access operator in the dart returns null if the
left-hand side is null else it will return the property of the right hand.
int? Age;
print(age?.isNegative);
● Null Assertion Operator ! → Sometimes dart doesn’t know whether a nullable value is null or not,
but if you are sure that it is not null, you can use the null assertion operator!.
● Note: You already learned that ! is not an operator before.
● The difference between not-operator ! and null assertion operator is you can use not-operator as a prefix.
a!==2 and you can use null assertion operator as a postfix.
Note: Wrong use of assertion operator ! may crash your program.
Late Keyword In Dart
● In some situations, variable, fields in class should be non-nullable but you can’t assign a value
immediately you can use a late keyword.
● What Happen When You Assign Late Keywords ?
● You can’t assign value now.
● You should assign a value before the variable is used.

late String name;


name = "John";

● Note: Using late means dart doesn’t initialize value right away, it only initializes when you access it for
the first time. This is also called lazy loading
OOP In Dart
● Object-oriented programming (OOP) is a programming method that uses objects and their interactions to
design and program applications. It is one of the most popular programming paradigms and is used in
many programming languages, such as Dart, Java, C++, Python, etc.
● In OOP, an object can be anything, such as a person, a bank account, a car, or a house. Each object has its
attributes (or properties) and behavior (or methods). For example, a person object may have the attributes
name, age and height, and the behavior walk and talk.
● Advantages
○ It is easy to understand and use.
○ It increases reusability and decreases complexity.
○ The productivity of programmers increases.
○ It makes the code easier to maintain, modify and debug.
○ It promotes teamwork and collaboration.
○ It reduces the repetition of code.
Features Of OOP
● Class
● Object
● Encapsulation
● Inheritance
● Polymorphism
● Abstraction

Note: The main purpose of OOP is to break complex problems into smaller objects. You will learn all these OOPs features later
in this dart tutorial.

● Object Oriented Programming (OOP) is a programming paradigm that uses objects and their interactions to design and
program applications.
● OOP is based on objects, which are data structures containing data and methods.
● OOP is a way of thinking about programming that differs from traditional procedural programming.
● OOP can make code more modular, flexible, and extensible.
● OOP can help you to understand better and solve problems
CLASS IN DART
● In object-oriented programming, a class is a blueprint for creating objects. A class defines the properties and
methods that an object will have.
● You can declare a class in dart using the class keyword followed by class name and braces {}.

class ClassName {
// properties or fields
// methods or functions
}

● The class keyword is used for defining the class.


● ClassName is the name of the class and must start with capital letter.
● Body of the class consists of properties and functions.
● Properties are used to store the data. It is also known as fields or attributes.
● Functions are used to perform the operations. It is also known as methods.
Object In Dart
● In object-oriented programming, an object is a self-contained unit of code and data.
Objects are created from templates called classes. An object is made up of
properties(variables) and methods(functions). An object is an instance of a class.
● Note: To create an object, you must create a class first. It’s a good practice to
declare the object name in lower case.
ClassName objectName = ClassName();
● Note: Once you create an object, you can access the properties and methods of the object
using the dot(.) operator.
● An object is an instance of a class. You can create multiple objects of the same class. If
you want to learn more about an object in Dart, you can read object in dart.
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.
● For example, the following code creates a Person class object and sets the initial values for the name
and age properties.
Person person = Person("John", 30);
● Things To Remember
○ The constructor’s name should be the same as the class name.
○ Constructor doesn’t have any return type.
● Note: When you create a object of a class, the constructor is called automatically. It is used to
initialize the values when an object is created.
INHERITANCE IN DART
● Inheritance is the concept of creating a new class with the basic blueprint of the existing class. The child
class will contain all the attributes and methods of the parent class as well can create its own.
● Advantages
○ It promotes reusability of the code and reduces redundant code.
○ It makes code simpler, cleaner and saves time and money on maintenance.
○ It facilitates the creation of class libraries.
○ It can be used to enforce standard interface to all children classes.

class ChildClassName extends ParentClassName

● Note: The extend keyword is used for inheriting from parent class.
ABSTRACT CLASS IN DART
● Abstraction is the class that contains one or more abstract methods (methods without implementation). It
has basic syntax needed for the class but are not complete. Hence, it declared abstract. You can declare
class abstracts using the keyword abstract in dart.
● Note: Abstract class may or may not contain abstract methods, but a class having at least one abstract
method must declare abstract.
● Key Features
○ Abstract class cannot be initialized.
○ It can have both abstract and non-abstract methods.
○ It is declared with abstract keyword.
○ It can be inherited using the extend keyword then the abstract methods need to be implemented.
abstract class ClassName {
//Body of abstract class
method1();
method2();
}
ENUM IN DART
● An enum or enumeration is used for defining named constant values in a dart programming language. An enumerated type is
declared using the keyword enum. It is used to hold the list of named constant values.
● Characteristics Of Enum
○ It must contain at least one constant value.
○ Enums are declared outside the class.
○ Used to store a large number of constant value.
○ Makes code more readable and simple.
○ It makes the code more reusable and makes it easier for developers.
○ Values inside enumeration type cannot be changed dynamically.

enum enum_name {
constant_value1,
constant_value2,
constant_value3
}
ENUM IN DART
● An enum or enumeration is used for defining named constant values in a dart programming language. An enumerated type is
declared using the keyword enum. It is used to hold the list of named constant values.
● Characteristics Of Enum
○ It must contain at least one constant value.
○ Enums are declared outside the class.
○ Used to store a large number of constant value.
○ Makes code more readable and simple.
○ It makes the code more reusable and makes it easier for developers.
○ Values inside enumeration type cannot be changed dynamically.

enum enum_name {
constant_value1,
constant_value2,
constant_value3
}

You might also like