ACS314
MOBILE APPLICATION
DEVELOPMENT
LESSON 2 (Read Chapter 2 &3)
MR ANTHONY WAMBUA
[email protected]
+254721589325
Reflection
Two Great phenomena :
❖ TIME
❖ CHANGE
Ecclesiastes 3:1-11
1: There is a time for everything, and a season for every activity
under the heavens.
Dart Programming Basics
• Features of Dart
• Variables.
• Operators.
• Datatypes.
• Branching statements.
• Looping statements.
Features of Dart
Introduction:
❖ Dart is backed by Google, it emerged in 2011. It is
opensource and similar to Java, C and JavaScript.
Characteristics/Features of Dart:
❖ Dart is a platform-independent language and supports all
operating systems such as Windows, Mac, Linux, etc.
❖ It is an open-source language.
❖ It is an object-oriented programming language and supports all
features of oops such as inheritance, interfaces, and optional
type features.
❖ Dart is very useful in building real-time applications because of
its stability.
❖ Dart comes with the dar2js compiler which transmits the Dart
code into JavaScript code that runs on all modern web
browser.
❖ The stand-alone Dart VM permits Dart code to run in a
command-line interface environment.
Before learning the Dart, Remember:
❖ Everything in Dart is treated as an object including, numbers,
Boolean, function, etc. like Python. All objects inherit from the
Object class.
❖ Dart tools can report two types of problems while coding,
warnings and errors. Warnings are the indication that your code
may have some problem, but it doesn't interrupt the code's
execution, whereas error can prevent the execution of code.
❖ Dart supports sound typing
❖ Dart supports generic types, like List<int>(a list of integers)
or List<dynamic> (a list of objects of any type).
Dart Variables
Rules:
❖ The first character should not be a digit.
❖ Special characters are not allowed except underscore (_) or a
dollar sign ($).
❖ Two successive underscores (__) are not allowed.
❖ The first character must be alphabet (uppercase or lowercase) or
underscore.
❖ Identifiers must be unique and cannot contain whitespace.
❖ They are case sensitive. The variable name Joseph and joseph will
be treated differently.
Dart Printing and String Interpolation
void main()
{
var name = "Peter";
var roll_no = 24;
print("My name is $name My roll number is $roll_no“);
//print(“My name is”+name+” my roll number is “+roll_no)
}
Output:
My name is Peter My roll number is 24
NB: $ is used for string interpolation
NB: if name was a property of an object called student
print("My name is ${student.name} “);
Dart Datatypes
• Dart is static typed and types of annotations language.
• Dart can infer a type and types annotations are optional.
• Number: int and double eg. int age=9, double pie=3.14
• You can use num, eg num=90.99 or num =9;
• Strings: e.g.: String name = "Jacob";
• Boolean e.g.: bool isClosed=true;
• Lists: The list is a collection of the ordered objects (value). The concept of list
is similar to an array e.g. var list = [1,2,3]
❖ Maps: The maps type is used to store values in key-value pairs
of any type. Each key is associated with its value. E.g. var
student = {'name': 'Joseph', 'age':25, 'Branch': 'Computer
Science’}
❖ Symbols: Objects which are used to refer an operator or
identifier that declare in a Dart program.
Lists..
• List integers = [1, 2, 3, 4, 5];
List doubles = [1.1, 1.2, 1.3];
List dynamicList = [1, 3.14, “Hello, world!”]; //any DT
Maps..
• Maps allow us to map one element to the other. It wi
ll enable us to store key-value pairs.
Declaring Variables in Dart
Using keyword var: e.g. var name = 'Andrew’. //Dart will infer (get the type)
from the value assigned to the variable name
With type annotation: Specifying the exact datatype.
int age;
late int age;
String msg = "Welcome to Java point“;
L1Q1: What are the inbuild Data types in Dart
Public/private variables
• Simply put, the public and private keywords do not e
xist in Dart. A variable is
public by default; we need to add an underscore to t
he variable name to mark it private. Code 2.29:
// This is public var a = 42;
// This is private var _b = 42;
Null safety
The Dart language enforces sound null safety.
Null safety prevents errors that result from unintentional access of variables set
to null
int a = 5;
int? a = 5; Added question mark which denotes that this variable can be null.
The first line indicates that the variable cannot be null, no matter how hard you try.
This means that this code snippet is valid:
int? a = 5; a = null;
int a = 5; // Compile error a = null;
This is not just for integers but for any type.
If you add a question mark in front of
any type, it becomes nullable, while a type without a question mark is non-nullable.
Late variables
• If have a variable that will be non
nullable but still cannot be
assigned at initialization, we can mark it late, m
eaning this will be assigned late, but
cannot be null once assigned.
• This variable assigned before initialization will t
hrow an error.
late int a; // Somewhere later a = 5;
Conditional and Loops
• If-else
• Switch
• While loop
• For loop
• For loop and collection iteration
3
1
For in ( More readable)
2
do while
while
L2Q2:Write a summary of the looping and decision statements used in dart
• The ideal way to iterate( loop through) a collection..
END