0% found this document useful (0 votes)
12 views65 pages

Unit: 1.1 Introduction To Flutter: Prof. Mehul Bhundiya

Uploaded by

sujalrabadiya7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views65 pages

Unit: 1.1 Introduction To Flutter: Prof. Mehul Bhundiya

Uploaded by

sujalrabadiya7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Mobile Application Development using Flutter

(MADF) (2101CS402)

Unit : 1.1
Introduction to
Flutter

Prof. Mehul Bhundiya


Computer Science & Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
+91 - 9428231065
Dart Programming Language
Dart Introduction
Dart Overview
 Dart is an open-source general-purpose programming language.
 It is originally developed by Google.
 The Dart SDK ships with its compiler – the Dart VM.
 SDK includes a utility -dart2js, a Transpiler that generates JavaScript equivalent of a Dart
Script.
 Transpiler (translates a source code from a language to another at the same level of
abstraction).
 Dart language is familier to Java Script, If you have worked on JavaScript, then it will help
you further to grasp the concepts of Dart quickly.
 Dart is an object-oriented language with C-style syntax which can optionally trans compile
into JavaScript.
 It supports interfaces, classes, abstraction, etc...

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 3
Dart Tools
 You may test your code online by using the online editor DartPad https://dartpad.dartlang.org/
.
 You may also code in different text editors like Windows Notepad, Notepad++, Emacs, vim
or vi, etc.
 Editors may vary from one Operating System to another.
 The source files are typically named with the extension .dart.
 The dart sdk can be downloaded from https://www.dartlang.org/install/archive.
 You can also different IDES Like VSCode, Android Studio, Eclipse, IntelliJ, etc..

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 4
Syntax
 Syntax defines a set of rules for writing programs.
 Every language specification defines its own syntax.
 A Dart program composed of
 Variables and Operators
 Classes
 Functions
 Expressions
 Decision Making and Loop
 Comments
 Libraries and Packages
 Typedefs
 Data structures represented as Collections / Generics

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 5
Syntax
Syntax
1 void main(){
2
3 print("Hello World!");
4
5 }

 The main() function is a predefined method in Dart.


 This method acts as the entry point to the application. It needs the main() method for
execution
 print() is a predefined function that prints the specified string or value to the standard output
i.e. the terminal.
 ToSyntax
execute a Dart program
1 dart file_name.dart

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 6
Variables & DataTypes
 One of the most fundamental characteristics of a programming language is the set of data
types it supports.
 The Dart language supports following datatypes.
 Numbers
 Strings
 Booleans
 Lists
 Maps
 Dynamic

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 7
Number
 Numbers in Dart are used to represent numeric literals.
 The Number Dart come in two types.
 Integer
 Double
 Integer:
 Integer values represent non-fractional values. Syntax
 For example, the value "10" is an integer. 1 int A = 20;
 Integer literals are represented using the int keyword.
 Double :
 The Double data type in Dart represents a 64-bit (double-precision) floating-point number.
 For example, the value "10.10".
 The keyword double is used to represent floating point literals. Syntax
1 double B = 30.0;

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 8
Number
Program Output
1 void main(){ 1 2
2 2 1.5
3 // declare an integer 3 Product = 3.34
4 int num1 = 2;
5
6 // declare a double value
7 double num2 = 1.5;
8
9 // print the values
10 print(num1);
11 print(num2);
12
13 var a1 = num.parse("1");
14 var b1 = num.parse("2.34");
15
16 var c1 = a1+b1;
17
18 print("Product = ${c1}");
19
20 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 9
Strings
 Strings represent a sequence of characters.
 String datatype used to store some data like name, address etc.
 A Dart String is a sequence of UTF-16 code units.
 The keyword String is used to represent string literals.
 String values are embedded in either single or double quotes.
Syntax
1 String string = 'Darshan University';

 You can use the plus (+) operator to concatenate strings:


Syntax
1 String string = 'Darshan‘ +
'University';

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 10
Strings
 You can use ${} to interpolate the value of Dart expressions within strings. The curly braces
can be omitted when evaluating identifiers.
Syntax
1 String str = ‘is at hadala‘;
2 print(‘Darshan University $str’);
3 print(‘Darshan University $
{str.toString()}’);

Program Output
1 void main(){ 1 Darshan University
2 String str = 'Darshan University'; 2 Darshan University is at hadala
3 String str1 = 'is at hadala';
4 print(str);
5 print(str + str1);
6 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 11
Boolean
 The Boolean data type represents Boolean values true and false.
 Dart uses the bool keyword to represent a Boolean value.
 These data types have two values that are True and False.
Syntax
1 bool isIttrue1 = true;

Program Output
1 void main(){ 1 false
2 String str = 'Darshan University';
3 String str1 = 'is at hadala';
4 bool val = (str==str1);
5 print(‘$val’);
6 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 12
Assessing Variable and Function
 we can access the fields and methods of the class using (.) operator with the object name.
Syntax
1 objectName.propName or objectName.methoName()

Example Example
1 class Student { 1 std.stdAge =24;
2 var stdName; 2 std.stdRoll_nu = 90001;
3 var stdAge; 3 //Accessing class Function
4 var stdRoll_nu; 4 std.showStdInfo();
5 5 }
6 // Class Function
7 showStdInfo() {
8 print("Student Name is : ${stdName}");
9 print("Student Age is : ${stdAge}"); Output
10 print("Student Roll Number is : $ 1 Student Name is : Darshan
11 {stdRoll_nu}"); 2 Student Age is : 24
12 } 3 Student Roll Number is : 498
13
14 void main() {
15 // Creating object called std
16 var std = new Student();
std.stdName = “Darshan"; #2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 13
Sound null safety
 Null Safety in simple words means a variable cannot contain a ‘null’ value.
 It prevents errors that result from unintentional access of variables set to null.
 There are two types on variables:
 Non-nullable types
 Nullable Types

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 14
Nullable & Non-Nullable Types
 Non-nullable types
 When we use null safety, all types are by default non-nullable.
 For example, an int variable must have an integer value.
Example
1 void main(){
2 int number;
3 number = 0;
4 }

 If a variable is non-nullable, it must always be set to a non-null value.


 Nullable types
 Nullable type (?) operators specify if a variable can be null.
 A nullable variable does not need to be initialized before being used.
Example
1 String? houseLocationName; // By default, it's set to null.
2 int? number = 36; // By default, it's set to non-null.
3 number = null; // It's possible to reassign it to null.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 15
Assertion/Bang Operators
 The Assertion/Bang Operator (!)
 It used to inform dart compiler that variable value is not null.
Example
1 int? anyNumber = 50;
2 int data = anyNumber!; // Because the value is not nullable, this is valid

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 16
Access Modifiers
 In Dart we don’t have keywords like public, protected, and private to control the access scope
for a property or method.
 It use _ (underscore) at the start of the variablename to make a data member private.
 In Dart, the privacy is at library level rather than class level.
 It means other classes and functions in the same library still have the access.
 So, a data member is either public (if not preceded by _) or private (if preceded by _).

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 17
Access Modifiers : Example
 For example, there is a library a.dart with a class named A. The class has two fields: first
(public) and _second (private).
Example
1 class A
2 {
3 String first;
4 String _second;
5 }
6
7 void main()
8 {
9 A a = new A();
10 a.first = 'New first';
11 a._second = 'New second';
12 print('${a.first}: ${a._second}');
13 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 18
List
 The data types list and map are used to represent a collection of objects.
 A List is an ordered group of objects.
 The List data type in Dart is synonymous to the concept of an array in other programming
languages.
 The dart:core library provides the List class that enables creation and manipulation of lists.
 Lists can be classified as
 Fixed Length List
 Growable List

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 19
Fixed Length List
 A fixed length list’s length cannot change at runtime.
Syntax
1 var list_name = new List(initial_size)

 The above syntax creates a list of the specified size.


 The list cannot grow or shrink at runtime.
Program Output
1 var lst = new List(3); 1 [12, 13, 11]
2 lst[0] = 12;
3 lst[1] = 13;
4 lst[2] = 11;
5 print(lst);

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 20
Growable List
 A growable list’s length can change at run-time.
Syntax
1 var list_name = new List();

Program Output
1 var lst = new List(); 1 [12, 13, 11]
2 lst[0] = 12;
3 lst[1] = 13;
4 lst[2] = 11;
5 print(lst);

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 21
Map
 The Map data type represents a set of values as key-value pairs.
 Keys and values on a map may be of any data type.
 It is a dynamic collection.
Syntax
1 var identifier = Map()
2 identifier[key] = value

Program Output
1 void main(){ 1 {First: Darshan, Second: University}
2 Map<String,dynamic> gfg = Map();
3 gfg['First'] = ‘Darshan';
4 gfg['Second'] = ‘University';
5 print(gfg);
6 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 22
Decision Making
 Decision-making statements allow programmers to choose which statement should be
executed under different conditions.
 There are five types of Decision Making statements available in dart.
 if statement
 if-else statement
 if else if statement
 switch case statement

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 23
if statement
 The if statement checks the given condition and if it returns true, the block of code is
executed. If not, it disregards the block of code.

Syntax
1 if ( condition )
2 {
3 // statement(s) to be executed
4 }

Program Output
1 void main(){ 1 8 is greater than 5
2 int a = 8;
3 // Condition is true
4 if (a > 5) {
5 print(" $a is greater than 5");
6 }
7 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 24
if-else statement
 The if-else statement tests the condition and, if it is true, it executes the if-block. But if it is
not, the else block is executed.
Syntax
1 if ( condition ){
2 // body of if
3 }
4 else {
5 // body of else
6 }

Program Output
1 void main() { 1 The condition is true
2 int a= 15;
3 if (a > 10) {
4 // This will be printed
5 print("The condition is true");
6 } else {
7 // This will not be printed
8 print("The condition is false");
9 }
10 }
#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 25
if else if statement
 The if else if statement checks the condition and, if it is true, executes the statements within
it.
 If it is not, other if conditions are check, and if they are true, they are executed.
 It’sSyntax
useful when we have to choose between more than two options.
1 if ( condition1 ){
2 // body of if
3 }
4 else if ( condition2 ){
5 // body of if
6 }
7 else if ( conditionN ){
8 // body of if
9 }
10 .
11 .
12 else {
13 // statement
14 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 26
if else if statement
Program Output
1 void main() 1 Condition 2 is true
2 {
3 int a = 15;
4 int b = 5;
5 if (a < b) {
6 print("Condition 1 is true");
7 }
8 else if (a > b) {
9 print("Condition 2 is true");
10 }
11 else if (a == b) {
12 print("Condition 3 is true");
13 }
14 else {
15 print("All the conditions are false");
16 }
17 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 27
switch case statement
 The switch case statement is a simplified form of a nested if-else statement.
 The switch case statement is used to eliminate the if-else statement’s long chain.
 The variable’s value is compared to the multiple cases, and if a match is found, a block of
statements associated with that case is executed.
Syntax
1 switch( expression ) {
2 case value1:{
3 // statement(s)
4 }
5 break;
6 case value2:{
7 // statement(s)
8 }
9 break;
10 .
11 .
12 default:{
13 //statement(s);
14 }
}
#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 28
switch case statement
Program Program
1 void main() { 20 default: {
2 var grade = "A"; 21 print("Invalid choice");
3 switch(grade) { 22 }
4 case "A": { 23 break;
5 print("Excellent"); 24 }
6 } 25 }
7 break;
8 case "B": {
9 print("Good");
10 } Output
11 break; 1 Excellent
12 case "C": {
13 print("Fair");
14 }
15 break;
16 case "D": {
17 print("Poor");
18 }
19 break;

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 29
Operators in Dart
 The operators are special symbols that are used to carry out certain operations on the
operands.
 Dart has numerous built-in operators used for different functions.
 Arithmetic Operators
 Relational Operators
 Type Test Operators
 Bitwise Operators
 Assignment Operators
 Logical Operators
 Conditional Operator
 Cascade Notation Operator

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 30
Arithmetic Operators
 Operators which are used to perform arithmetic operation on the operands.

Operator Name Description

+ Addition Use to add two operands

– Subtraction Use to subtract two operands

Unary Minus It is Use to reverse the sign of the


-expr
expression
* Multiply Use to multiply two operands

/ Division Use to divide two operands

Division (Integer Division) Use two divide two operands but give
~/
output in integer
% Modulus Use to give remainder of two operands

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 31
Arithmetic Operators
Program Program
1 void main() 21 // Division of a and b
2 { 22 var g = b / a;
3 int a = 2; 23 print("The quotient of a and b is $g");
4 int b = 3; 24
5 25 // Using ~/ to divide a and b
6 // Adding a and b 26 var h = b ~ / a;
7 var c = a + b; 27 print("The quotient of a and b is $h");
8 print("Sum of a and b is $c"); 28
9 29 // Remainder of a and b
10 // Subtracting a and b 30 var i = b % a;
11 var d = a - b; 31 print("The remainder of a and b is $i");
12 print("The difference between a and b is $d"); 32 }
13
14 // Using unary minus Output
15 var e = -d; 1 Sum of a and b is 5
16 print("The negation between a and b is $e"); 2 The difference between a and b is -1
17 3 The negation between a and b is 1
18 // Multiplication of a and b 4 Product of a and b is 6
19 var f = a * b; 5 The quotient of a and b is 1.5
20 print("The product of a and b is $f"); 6 The quotient of a and b is 1
7 The remainder of a and b is 1

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 32
Relational Operators
 Operators which are used to perform relational operation on the operands.

Operator Name Description


Greater than operand is bigger and give result as
>
boolean expression.
Less than operand is smaller and give result as
<
boolean expression.
Greater than or equal to operand is greater or equal to each other
>=
and give result as boolean expression.
less than equal to operand is less than or equal to each other
<=
and give result as boolean expression.
Equal to operand are equal to each other or not
==
and give result as boolean expression.
Not Equal to operand are not equal to each other or not
!=
and give result as boolean expression.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 33
Relational Operators
Program Program
1 void main() 21 // Equality between a and b
2 { 22 var g = b == a;
3 int a = 2; 23 print("a and b are equal is $g");
4 int b = 3; 24
5 25 // Unequality between a and b
6 // Greater between a and b 26 var h = b != a;
7 var c = a > b; 27 print("a and b are not equal is $h");
8 print("a is greater than b is $c"); 28 }
9
10 // Smaller between a and b
11 var d = a < b; Output
12 print("a is smaller than b is $d"); 1 a is greater than b is false
13 2 a is smaller than b is true
14 // Greater than or equal to between a and b 3 a is greater than b is false
15 var e = a >= b; 4 a is smaller than b is true
16 print("a is greater than b is $e"); 5 a and b are equal is false
17 6 a and b are not equal is true
18 // Less than or equal to between a and b
19 var f = a <= b;
20 print("a is smaller than b is $f");

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 34
Type Test Operators
 Operators which are used to perform comparison operation on the operands.

Operator Name Description


Is Gives boolean value true as output if the
is
object has specific type
is not Gives boolean value false as output if the
is!
object has specific type

Program Output
1 void main() 1 true
2 { 2 true
3 String a = 'GFG';
4 double b = 3.3;
5
6 // Using is to compare
7 print(a is String);
8
9 // Using is! to compare
10 print(b is !int);
11 }
#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 35
Bitwise Operators
 Operators which are used to perform bitwise operation on the operands.

Operator Name Description


Bitwise AND Performs bitwise and operation on two
&
operands.
Bitwise OR Performs bitwise or operation on two
|
operands.
Bitwise XOR Performs bitwise XOR operation on two
^
operands.
Bitwise NOT Performs bitwise NOT operation on two
~
operands.
Left Shift Shifts a in binary representation to b bits
<<
to left and inserting 0 from right.
Right Shift Shifts a in binary representation to b bits
>>
to left and inserting 0 from left.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 36
Bitwise Operators
Program Program
1 void main() 21 // Performing left shift on a
2 { 22 var g = a << b;
3 int a = 5; 23 print(g);
4 int b = 7; 24
5 25 // Performing right shift on a
6 // Performing Bitwise AND on a and b 26 var h = a >> b;
7 var c = a & b; 27 print(h);
8 print(c); 28 }
9
10 // Performing Bitwise OR on a and b
11 var d = a | b; Output
12 print(d); 1 5
13 2 7
14 // Performing Bitwise XOR on a and b 3 2
15 var e = a ^ b; 4 4294967290
16 print(e); 5 640
17 6 0
18 // Performing Bitwise NOT on a
19 var f = ~a;
20 print(f);

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 37
Assignment Operators
 Operators which are used to perform comparison operation on the operands.

Operator Name Description


Equal to Use to assign values to the expression or
=
variable
??= Assignment operator Assign the value only if it is null.

Program Output
1 void main() 1 35
2 { 2 12
3 int a = 5; 3 12
4 int b = 7;
5 // Assigning value to variable c
6 var c = a * b;
7 print(c);
8 // Assigning value to variable d
9 var d;
10 d ? ? = a + b; // Value is assign as it is null
11 print(d);
12 }
#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 38
Logical Operators
 Operators which are used to logically combine two or more conditions of the operands.

Operator Name Description


And Operator Use to add two conditions and if both are
&&
true than it will return true.
Or Operator Use to add two conditions and if even
|| one of them is true than it will return
true.
! Not Operator It is use to reverse the result.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 39
Logical Operators
Program Output
1 void main() 1 false
2 { 2 true
3 int a = 5; 3 true
4 int b = 7;
5
6 // Using And Operator
7 bool c = a > 10 && b < 10;
8 print(c);
9
10 // Using Or Operator
11 bool d = a > 10 || b < 10;
12 print(d);
13
14 // Using Not Operator
15 bool e = !(a > 10);
16 print(e);
17 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 40
Conditional Operators
 Operators which are used to logically combine two or more conditions of the operands.

Operator Name Description


And Operator Use to add two conditions and if both are
&&
true than it will return true.
Or Operator Use to add two conditions and if even
|| one of them is true than it will return
true.
! Not Operator It is use to reverse the result.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 41
Conditional Operators
Program Output
1 void main() 1 false
2 { 2 true
3 int a = 5; 3 true
4 int b = 7;
5
6 // Using And Operator
7 bool c = a > 10 && b < 10;
8 print(c);
9
10 // Using Or Operator
11 bool d = a > 10 || b < 10;
12 print(d);
13
14 // Using Not Operator
15 bool e = !(a > 10);
16 print(e);
17 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 42
Conditional/Ternary Operator
 The conditional operator is a shorthand version of an if-else condition.
 There are two types of conditional operator syntax in Dart.
 One with a null safety check and the other is the same old one we encounter normally.
Syntax
1 condition ? expressionOne : expressionTwo;

 The above syntax implies that if a certain condition evaluates to true then we evaluate the
expressionOne first and then the expressionTwo.
Program Output
1 void main(){ 1 Answer is 10
2 var ans = 10;
3 ans == 10 ? print("Answer is 10") : print("Oh no!");
4 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 43
Null Aware Operator
 It depicts a conditional statement that is similar to a conditional operator statement.
 when you want to evaluate and return an expression if another expression resolves to null.
 It is also called the if-null operator.
 The null-aware operator is ??, which returns the expression on its left unless that expression’s
value is null.
Syntax
1 expression1 ?? expression2

 The only difference is that in the above syntax if expression1 is not null, then it gets
evaluated else expression2 is evaluated.
Program Output
1 void main(){ 1 Hello
2 var b;
3 String a = b ?? 'Hello';
4 print(a);
5 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 44
Cascade Notation Operators
 This operator allows you to perform a sequence of operation on the same element.
 It allows you to perform multiple methods on the same object.
Operator Name Description
Cascading Operator It is used to perform multiple methods on
..
the same object.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 45
Cascade Notation Operators
Program Program
1 class GFG { 1
2 var a; 2 // Without using Cascade Notation
3 var b; 3 geek1.set(1, 2);
4 4 geek1.add();
5 void set(x, y) 5
6 { 6 // Using Cascade Notation
7 this.a = x; 7 geek2..set(3, 4)
8 this.b = y; 8 ..add();
9 } 9 }
10
11 void add()
12 { Output
13 var z = this.a + this.b; 1 3
14 print(z); 2 7
15 }
16 }
17
18 void main()
19 {
20 // Creating objects of class GFG
21 GFG geek1 = new GFG();
22 GFG geek2 = new GFG();
#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 46
Loops
 A looping statement is used to repeat a particular set of commands until certain conditions are
not completed.
 There are different types of loops available in Dart.
 for loop
 for… in loop
 for each loop
 while loop
 do-while loop

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 47
For Loop
 For loop in Dart is similar to that in other programming languages and also the flow of
execution is the same as that in other programming languages.
Syntax
1 for(initialization; condition; text expression){
2 // Body of the loop
3 }

 Control flow:
 1) initialization
 2) Condition
 3) Body of loop
 4) Test expression
 The first is executed only once i.e in the beginning while the other three are executed until
the condition turns out to be false.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 48
For Loop
Program Output
1 void main() 1 Darshan University
2 { 2 Darshan University
3 for (int i = 0; i < 5; i++) { 3 Darshan University
4 print(‘Darshan University'); 4 Darshan University
5 } 5 Darshan University
6 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 49
For…in Loop
 The for...in loop is used to loop through an object's properties.
Syntax
1 for (variablename in object){
2 //statement or block to execute
3 }

 In each iteration, one property from the object is assigned to the variable.
 This loop continues till all the properties of the object are exhausted.
Program Output
1 void main() { 1 12
2 var obj = [12,13,14]; 2 13
3 for (var prop in obj) { 3 14
4 print(prop);
5 }
6 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 50
for each loop
 The for-each loop iterates over all elements in some collection and passes the elements to
some specific function.
Syntax
1 collection.foreach(void f(value))

 f( value): It is used to make a call to the f function for each element in the collection.
Program Output
1 void main() { 1 12
2 var obj = [12,13,14]; 2 13
3 obj.forEach((var num)=> print(num)); 3 14
4 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 51
Functions
 Functions are the building blocks of readable, maintainable, and reusable code.
 A function is a set of statements to perform a specific task.
 Functions organize the program into logical blocks of code.
 Once defined, functions may be called to access code.
 This makes the code reusable, easy to read & maintain the program’s code.
Syntax
1 return_type function_name ( parameters ) {
2 // Body of function return value;
3 }

 A function declaration tells the compiler about a function's name, return type, and parameters.
 function_name defines the name of the function.
 return_type defines the datatype in which output is going to come.
 return value defines the value to be returned from the function.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 52
Functions Call
Syntax
1 function_name (argument_list);

 function_name defines the name of the function.


 argument_list is the list of the parameter that the function requires.
Program Output
1 int add(int a, int b) 1 30
2 {
3 // Creating function
4 int result = a + b;
5 // returning value result
6 return result;
7 }
8 void main()
9 {
10 // Calling the function
11 var output = add(10, 20);
12
13 // Printing output
14 print(output);
15 }
#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming
Prof. Mehul Bhundiya 53
Arrow/Lambda Function
 The arrow function allows us to create with single expression.
 We can omit the curly brackets and the return keyword.
Syntax
1 return_type function_name(parameters...) => expression;

 return_type consists of datatypes like void,int,bool, etc..


 function_name defines the name of the function.
 parameters are the list of parameters function requires.
Program Output
1 void main() { 1 The perimeter of rectangele is 208
2 perimeterOfRectangle(47, 57);
3 }
4
5 // Arrow Syntax
6 void perimeterOfRectangle(int length, int breadth) => print('The perimeter of
7 rectangele is ${2 * (length + breadth)}');

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 54
Optional Parameter Function
 Function overloading is not supported in Dart at all.
 Optional parameters are those parameters that don't need to be specified when calling the
function.
 Optional parameters allow us to pass default values to parameters that we define.
 There are two types of optional parameters
 Ordered (positional) optional parameters
 Named optional parameters
 Anonymous function

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 55
Ordered (positional) optional parameter Function
 The square brackets [] are used to specify optional positional parameters.
 Optional parameters can be used when arguments need not be compulsorily passed for a
function’s execution.
Program Output
1 void main() { 1 4
2 print(pow(2, 2)); 2 4
3 print(pow(2, 3)); 3 9
4 print(pow(3));
5 }
6 int pow(int x, [int y = 2]) {
7 int r = 1;
8 for (int i = 0; i < 2; i++) {
9 r *= x;
10 }
11 return r;
12 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 56
Named optional parameters Function
 Optional named parameters are specified insice curly {} brackets.
 When passing the optional named parameter, we have to specify both the parameter name and
value, separated with colon.
 It is necessary for you to use the name of the parameter if you want to pass your argument.
Program Output
1 void main() { 1 4
2 var name = "John Doe"; 2 4
3 var occupation = "carpenter"; 3 9
4 info(name, occupation: occupation);
5 }
6 void info(String name, {String occupation}) {
7 print("$name is a $occupation");
8 }

 The info function takes an optional named argument as its second argument.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 57
Anonymous function
 Anonymous functions do not have a name.
 An anonymous function can have zero or more parameters with optional type annotations.
Syntax
1 (parameter_list) {
2 statement(s)
3 }

Program Output
1 void main() { 1 Example of anonymous function
2 var list = ["James","Patrick","Mathew","Tom"]; 2 0: James
3 print("Example of anonymous function"); 3 1: Patrick
4 list.forEach((item) { 4 2: Mathew
5 print('${list.indexOf(item)}: $item'); 5 3: Tom
6 });
7 }
8

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 58
Nested function
 A nested function, also called an inner function.
 It is a function defined inside another function.
Program Output
1 void main() { 1 John Doe is a gardener
2 String buildMessage(String name, String occupation) {
3 return "$name is a $occupation";
4 }
5 var name = "John Doe";
6 var occupation = "gardener";
7 var msg = buildMessage(name, occupation);
8 print(msg);
9 }

 We have a helper buildMessage function which is defined inside the main function.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 59
function as parameter
 A function can be passed to other functions as a parameter.
 This function is called a higher-order function.
Program Output
1 void main() { 1 11
2 exec(10, plusOne); 2 100
3 exec(10, double);
4 }
5
6 void exec(int i, Function f) {
7 print(f(i));
8 }
9
10 int plusOne(int i) => i + 1;
11 int double(int i) => i * i;

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 60
Class
 Dart is an object-oriented programming language.
 It supports the concept of class, object … etc.
 We use the class keyword to define class.
Syntax
1 class ClassName {
2 // Body of class
3 }

 Classes are the blueprint of the object.


 A class can contain fields, functions, constructors, etc.
 It is a wrapper that binds/encapsulates the data and functions together; that can be accessed
by creating an object.

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 61
Example
Example
1 void main() {
2 // Defining class
3 class Student {
4 var stdName;
5 var stdAge;
6 var stdRoll_nu;
7
8 // Class Function
9 showStdInfo() {
10 print("Student Name is : ${stdName}");
11 print("Student Age is : ${stdAge}");
12 print("Student Roll Number is : ${stdRoll_nu}")
13 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 62
Object
 Dart is object-oriented programming, and everything is treated as an object.
 An object is a variable or instance of the class used to access the class's properties.
 Objects have two features - state and behavior.
Syntax (with new Keyword)
1 var object_name = new ClassName(<constructor_arguments>);
OR
Syntax (without new Keyword)
1 var object_name = ClassName(<constructor_arguments>);

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 63
Object
Example
1 void main() {
2 var std = Student();
3 }
4
5 // Defining class
6 class Student {
7 var stdName;
8 var stdAge;
9 var stdRoll_nu;
10
11 // Class Function
12 showStdInfo() {
13 print("Student Name is : ${stdName}");
14 print("Student Age is : ${stdAge}");
15 print("Student Roll Number is : ${stdRoll_nu}")
16 }

#2101CS402 (MDAF)  Unit 1 – Introduction to dart programming


Prof. Mehul Bhundiya 64
Thank You

You might also like