Chapter 3 Advanced Concepts in Dart
Chapter 3 Advanced Concepts in Dart
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.
● 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
}
● 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
}