Module 1 Introduction and Dart Programming
Module 1 Introduction and Dart Programming
Development:
Flutter
Design
Development
Testing
Launch
Marketing
Types of Mobile app development
Native
Web
Hybrid
10
Native Apps
12
13
14
15
`
16
17
18
19
20
21
Web Apps
23
24
25
Comparing Native Apps with Web Apps
26
28
What is Flutter?
• Flutter is a free and open-source mobile UI framework
created by Google and released in May 2017
• Allows you to create a native mobile application with only one
codebase.
Use one programming language and one codebase to create
two different apps (for iOS and Android)
• System requirements(minimum)
Operating System: Windows 7 SP1 or later (64-bit)
Disk Space: 400 Mb (Except disk space of IDE/tools)
RAM: 4 GB
Tools:
• Flutter depends on these tools being available in your environment
• Windows PowerShell
1. Get the Flutter SDK
• Step 1- Go to URL https://flutter.dev/docs/get-
started/install/windows and download the latest Flutter SDK.
• Step 2- Unzip the zip file and place the contained flutter in the
installation location for the Flutter SDK (For example,
C:/src/flutter; do not install Flutter in a directory like C:/Program
Files/ that requires relevant privileges).
• Cons of Dart
• Dart is fairly new to the programmers and rarely used in the
market.
• Dart has very limited resources online and it's hard to find
solutions to problems.
Dart Basics: Variables
• Comments
• Booleans
• Numbers
• Strings
• Const Variables
Comments
• Help the readability of the code
• Used to describe the logic and dependencies of the app
• Non executable lines of code
• There are three types of comments:
• Single-line (add a short description) //…….
• Multiline (long descriptions that span multiple lines)/*….*/
• Documentation (fully document a piece of logic, usually
giving detailed explanations and sample code in the
comments /// []
Comments
Multiline comments
/*……..*/
Single Line
//
Variables
• Variables store references to a value
• Some of the built-in variable types are numbers, strings,
Booleans, lists, maps etc.
• You can use var to declare a variable without specifying the
type.
Dart infers the type of variable automatically
• Declaring the variable type makes for better code readability,
and it’s easier to know which type of value is expected.
Instead of using var, use the variable type expected:
double, String, and so on.
• An uninitialized variable has a value of null
• Use final or const keywords when the variable is not intended
to change the initial value
Declaring Variables
• In Dart, all variables are declared public (available to all) by
default
• Starting the variable name with an underscore (_), you can
declare it as private.
By declaring a variable private, you are saying it cannot
be accessed from outside classes/functions
• Note some built-in Dart variable types are lowercase like
double and some uppercase like String
• Use final keyword when the value is assigned at runtime
• Use const keyword when the value is known at compile time
(in code) and will not change at runtime.
Declaring Variables Rules
• Special characters are not allowed(@,&,#). There is an
exception for dollar sign($) and underscore (_)
• A keyword cannot be used as a variable name for example int ,
double cannot be used as a variable name.
• Variables are case sensitive
• First character of a variable should not be a digit and must
always be an alphabet.
• Blank spaces cannot be used
• A combination of numbers and alphabet can be used to name
a variable for example , name123 can be used as a variable
name.
Booleans
• Dart provides an inbuilt support for the Boolean data type.
• The Boolean data type in DART supports only two values –
true and false
• The keyword bool is used to represent a Boolean literal in
DART.
• The syntax for declaring a Boolean variable in DART is as given
below −
• bool var_name = true
• bool var_name = false
Booleans
Booleans(contd..)
To retrieve(interpolate) value
stored in variable
{$var_name} or ${var_name}
Numbers
• Dart numbers can be classified as −
• int − Integer of arbitrary size used to represent whole
numbers.
• double − 64-bit (double-precision) floating-point numbers
used to represent fractional numbers
• The num type is inherited by the int and double types.
• The dart core library allows numerous operations on numeric
values.
• The syntax for declaring a number is as given below −
• int var_name; // declares an integer variable
• double var_name; // declares a double variable
Numbers(contd..)
Strings
• Declaring variables as String allows values to be entered as a
sequence of text characters.
• To add a single line of characters, you can use single or double
quotes like 'car' or "car".
Ex:
String defaultMenu = 'main';
String defaultMenu = “main”;
Output
value greater than 15
Flow Control
• Assert
• Selection Statements [If…else, Switch case]
• Iterations Statements [Loops: for, while,
do..while]
• Jump Statements [break, continue]
Flow Control
• Order in which instructions, statements and function calls
being executed or evaluated when a program is running
• In dart, statements inside code are generally executed
sequentially from top to bottom in order that they appear
• During program execution, you may execute or skip certain
set of instructions based on condition, jump to another
statement or execute a set of statement block repeatedly
Flow control statements are used to alter, redirect or
control flow of program execution based on application
logic
Assert
• Useful debugging tool, is used for testing boolean
conditions.
• An assert statement disrupt normal execution if a
boolean condition is false.
• If the boolean expression is true, then the code continues
to execute normally.
1. Selection Statements
• Allow you to control flow of program during the runtime on
the basis of outcome of an expression or state of a variable
void main(){
print("Dart Program To Add Two Numbers Using Function.");
var sum = add(10,20);
print("Sum Of Given No. Is : ${sum}");
}
• What is StackTrace?
• How to create our own exception handling class
Exception Handling
• Exception is a runtime unwanted event that disrupts the
flow of code execution or application crashes
It can be occurred because of programmer’s mistake
or by wrong user input.
• To handle such events at runtime is called Exception
Handling.
Types of Exceptions in Dart
Exception
try..catch..finally
• In Dart Exceptions can be handle via
Try: In the try block, we write the logical code that can produce
the exception
Catch: Catch block is written with try block to catch the general
exceptions: In other words, if it is not clear what kind of
exception will be produced. Catch block is used.
On: On block is used when it is 100% sure what kind of
exception will be thrown.
Finally: The finally part is always executed but it is not
mandatory.
try clause
Case 1: when you know exception to be thrown use On clause
try…catch clause
• Case 2: when you do not exception, use catch clause
try…catch clause
• Case 3: Using Stack Trace to know events occurred
before exception was thrown
try..catch..finally
• Case 4: Whether there is an exception or not, finally clause is
always executed
try..catch..finally
• Case 4: Whether there is an exception or not, finally clause is
always executed
Throwing Exception
• The throw keyword is used to explicitly raise an exception.
A raised exception should be handled to prevent the
program from exiting abruptly.
• The syntax for raising an exception explicitly is −
throw new Exception_name()
Throwing Exception
Throwing Exception
Custom Exceptions
• Every exception type in Dart is a subtype of the built-in
class Exception.
• Dart enables creating custom exceptions by extending the
existing ones.
• The syntax for defining a custom exception is as given below −
class Custom_exception_Name implements Exception
{ // can contain constructors, variables and methods }
Custom Exceptions(contd..)
Dart: Intermediate
• Dart is an object oriented programming language which
supports concepts like:
• Classes
• Scope
• Packages(Imports)
• Polymorphism
• Inheritance
• Generics
Classes
Dart Classes
• Dart is an object oriented programming language and
supports the concept of class, objects, interfaces, inheritance
etc.
• Class can be defined as blueprint or prototype of associated
objects
• Class is a wrapper that binds/encapsulates the data and
methods together, which can be later accessed by objects of
that class
Class can be user defined data type that describe
behavior and characteristics shared by all of its instances
• Once a class has been defined, we can create
objects(instances) of that class which has access to class
properties and methods
Dart Classes(contd..)
Declaring a Class in Dart
• In dart, class can be defined using class keyword followed by
class name; and class body enclosed by pair of curly braces {}
Fields
method
Dart Classes(contd..)
Creating Class objects in Dart
• Once class has been defined, we can create objects(instance)
of class which has access to class fields and function
• Syntax:
var objectName = new ClassName(<constructor_arguments>);
• Example:
var emp = new Employee();
Dart Classes(contd..)
Accessing Instance
class Employee {
Variables and Functions var empName;
• In dart, once instance of class var empAge;
is created, we can access var empSalary;
properties and methods of
class using property/method showEmpInfo(){
name separated by print("Employee Name Is : ${empName}");
print("Employee Age Is : ${empAge}");
dot(.)operator after instance
print("Employee Salary Is : ${empSalary}");
name
}
}
void main(){
var emp = new Employee();
emp.empName = "John";
emp.empAge = 30;
emp.empSalary = 45000;
print("Dart Access Class Property and Method");
emp.showEmpInfo();
}
Classes(1)
2. Parameterized Constructor
3. Named Constructor
Default Constructor or no-arg Constructor
• As name specifies the constructor that has no parameters is
known as default constructor
• If you don’t create constructor for a class, compiler will
automatically create default constructor for the class
• If we create a constructor with no-arguments or arguments
then compiler will not create a default constructor
• Default constructor provide default values to member
variables
void main() {
Employee emp = new Employee();
}
class Employee{
Employee() {
print(“Default Constructor of Employee class ");
}
}
Default Constructor or no-arg Constructor(1)
Default Constructor or no-arg Constructor(2)
Parameterized Constructor
• Constructors can also take parameters which is used to
initialize instance variables
• A constructor that accepts parameter is called parameterized
constructor
• If we want to initialize instance variables with our own values,
then we are required to use parameterized constructor
void main() {
print("Dart Parameterized Constructor");
Employee emp = new Employee('EMP001');
}
class Employee{
Employee(String empCode) {
print(empCode);
}
}
Parameterized Constructor(1)
Parameterized Constructor(2)
Named Constructors
• In Dart, named constructor allows a class to define multiple
constructors void main() {
class Employee {
String emp_code;
Employee(String emp_code) {
this.emp_code = emp_code;
print("Dart this Keyword Example.");
print("The Employee Code is : ${emp_code}");
}
}
this keyword(contd..)
this keyword(contd..)
Output
Correct??
this keyword(contd..)
this keyword(contd..)
Variable Scope
Private and Public Variables
• Public variables
Variables that are visible to all classes
• Private variables
Variables that are visible only to the class to which they
belong
They are defined using underscore symbol(eg: int _age)
Private and Public Scope
Private variables
Private and Public Scope(contd..)
Private and Public Scope(contd..)
Private and Public Scope(private variable
modified)
Private and Public Scope(private method declared)
Private and Public Scope(private method
accessed)
Private and Public Scope(solution: private
method accessed through encapsulation)
Private and Public Scope(solution: private
method accessed through encapsulation)
Getters and Setters
• Getters and Setters are special class methods that is used to
initialize and retrieve the values of the class field respectively
• Setter method is used to initialize or set respective class fields
• Getter method is used to retrieve respective class fields
• All classes have default getter and setter method associated
with it
However, you are free to override default ones by
implementing getter and setter method explicitly
Default Getter and Setter
Custom Getters and Setters(contd..)
Custom Getters and Setters(contd..)
Custom Getters and Setters(contd..)
Static members
• Static keyword is used for memory management of global data
members
• Static keyword can be applied to fields and methods of a class
• Static variables and methods are part of class instead of
specific instance
• Static data members can be accessed without creating an
object of class
Simply put class_name before static variable or method
name to use them
Static members(contd..)
Static Variables
• Belongs to class instead of specific instance(objects)
• It is common to all instances of class
• Memory allocation to static variable happen only once in class
area at time of class loading
Static members(contd..)
Static Members(contd..)
Static Members(contd..)
Static Members(contd..)
Static Members(contd..)
Static Members(contd..)
Static Method
• Static method belong to class instead of class instances
• Static method is allowed to access static variables of class
and can invoke only static methods of the class
Static Members(contd..)
Static Members(contd..)
Packages
Dart Packages
• Set of dart program organized in an independent,
reusable unit
Inheritance
Inheritance (contd..)
• Syntax:
class child_class extends parent_class {
//body of child class
}
2 3
Output???
Mixins(contd..)
If this class contains any identical methods to mixins or super class then the
methods in this class would be called first
Functional Programming in Dart
• Lambda Expression
• Lexical Closures
Lambda Expressions
• A function without a name
• Also known as anonymous function or lambda
;
Lambda Expressions(contd..){1st way}
Lambda Expressions(contd..){2nd way}
Higher Order Functions
• Higher Order Functions:
• Can accept function as a parameter
• Can return a function
• Or can do both
Higher Order Functions (contd..)
Higher Order Functions (contd..)
Closures
• Closure is a special function
• Within a closure, you can mutate(modify) the values of
variables present in parent scope
• In Java 8, you are not allowed to modify parent scope
variables
Closure(contd..)
Closure(contd..)
Generics
• Introduction
• Examples of Generics
• Generic Class
• Generic Method
Generics
• Dart is an optionally typed language.
• Collections in Dart are heterogeneous by default.
In other words, a single Dart collection can host
values of various types
• However, a Dart collection can be made to hold
homogenous values with help of generics concept