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

Chapter 2 Introduction To Dart

The document provides an overview of the Dart programming language and its features. It discusses Dart variables and types, built-in types like numbers, strings, booleans, comments, operators, and properties and methods of strings. Dart is an object-oriented language developed by Google that can be used to develop mobile, web, and desktop applications. It offers features like null safety and asynchronous programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Chapter 2 Introduction To Dart

The document provides an overview of the Dart programming language and its features. It discusses Dart variables and types, built-in types like numbers, strings, booleans, comments, operators, and properties and methods of strings. Dart is an object-oriented language developed by Google that can be used to develop mobile, web, and desktop applications. It offers features like null safety and asynchronous programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Mobile

Application
Development
Introduction to
Dart

Chapter Two
● Introduction to Dart
Outline ● Dart Variables and Types
● Using Dynamic Types
● Understanding control flows
● Loops in Dart
● Understanding Functions
● Data structures, collections,
Dart
● Dart is a client-optimized, object-oriented, modern programming language to build
apps fast for many platforms like android, iOS, web, and desktop, etc.
● Client optimized means optimized for crafting a beautiful user interface and high-
quality experiences.
● Google developed Dart as a programming language.
● A solid understanding of Dart is necessary to develop high-quality apps with flutter.
Features Of Dart
● Free and open-source.
● Object-oriented programming language.
● Used to develop android, iOS, web, and desktop apps fast.
● Can compile to either native code or javascript.
● Offers modern programming features like null safety and asynchronous programming.
● You can even use Dart for servers and backend
Difference Between Dart & Flutter
● Dart is client optimized, object-oriented programming language. It is popular nowadays
because of flutter. It is difficult to build complete apps only using Dart because you have to
manage many stuff yourself.

● Flutter is a framework that uses dart programming language. With the help of flutter, you
can build apps for android, iOS, web, desktop, etc. The framework contains ready-made
tools to make apps faster.
History of Dart
● Google developed Dart in 2011 as an alternative to javascript.
● Dart 1.0 was released on November 14, 2013.
● Dart 2.0 was released in August 2018.
● Dart gained popularity in recent days because of flutter.
Dart Variables and Types
A variable is “a named space in the memory” that stores values. In other words, it acts a
container for values in a program. Variable names are called identifiers. Following are the
naming rules for an identifier −

● Variable names are case sensitive, i.e., a and A are different.


● A variable name can consist of letters and alphabets.
● A variable name cannot start with number.
● Keywords are not allowed to use as a variable name.
● Blank spaces are not allowed in a variable name.
● Special characters are not allowed except for the underscore (_) and the dollar
($) sign.
Dart Variables and Types
var name = 'Bob';
➔ Variables store references. The variable called name contains a reference to a String object with a
value of “Bob”.
➔ In dart, var automatically finds a data type. In simple terms, var says if you don’t want to specify a
data type, I will find a data type for you.
➔ The type of the name variable is inferred to be String, but you can change that type by specifying
it. If an object isn’t restricted to a single type, specify the Object type (or dynamic if necessary).

Object name = 'Bob';


➔ Another option is to explicitly declare the type that would be inferred:
String name = 'Bob';
➔ You don’t have to initialize a local variable where it’s declared, but you do need to assign it a value
before it’s used.
Final and const
➔ If you never intend to change a variable, use final or const, either instead of var or in
addition to a type. A final variable can be set only once; a const variable is a compile-time
constant. (Const variables are implicitly final.)
◆ final name = 'Bob'; // Without a type annotation
◆ final String nickname = 'Bobby';

➔ You can’t change the value of a final variable.


➔ Use const for variables that you want to be compile-time constants. If the const variable is at the
class level, mark it static const.
◆ const bar = 1000000; // Unit of pressure (dynes/cm2)
◆ const double atm = 1.01325 * bar; // Standard atmosphere
Built-in types
1. Numbers : Dart numbers come in two flavors:
a. int⇒ Integer values no larger than 64 bits. on native platforms, values can be from -
263 to 263 - 1.
b. double⇒ 64-bit (double-precision) floating-point numbers, as specified by the IEEE
754 standard.
● Both int and double are subtypes of num
○ var x = 1;
○ var hex = 0xDEADBEEF;
○ var y = 1.1;
○ var exponents = 1.42e5;
● You can also declare a variable as a num. If you do this, the variable can have both integer and
double values.
○ num x = 1; // x can have both int and double values
○ x += 2.5;
Built-in types
2. Strings: A Dart string (String object) holds a sequence of UTF-16 code units. You can use
either single or double quotes to create a string:

○ var s1 = 'Single quotes work well for string literals.';


○ var s2 = "Double quotes work just as well.";
○ var s3 = 'It\'s easy to escape the string delimiter.';
○ var s4 = "It's even easier to use the other delimiter.";

● Another way to create a multi-line string: use a triple quote with either single or
double quotation marks:
○ var s1 = '''
You can create
multi-line strings like this one.
''';
○ var s2 = """This is also a
multi-line string.""";
Built-in types
3. Booleans: To represent boolean values, Dart has a type named
bool. Only two objects have type bool: the boolean literals true
and false, which are both compile-time constants.
○ bool isMarried = true;
○ print("Married Status: $isMarried");
Comments in Dart
● Single-Line Comment: For commenting on a single line of code.
E.g. // This is a single-line comment.
● Multi-Line Comment: For commenting on multiple lines of code.
E.g. /* This is a multi-line comment. */
● Documentation Comment: For generating documentation or
reference for a project/software package. E.g. ///
Arithmetic Operators in Dart
Operator Symbol Operator Name Description

+ Addition For adding two operands

- Subtraction For subtracting two operands

-expr Unary Minus For reversing the sign of the expression

* Multiplication For multiplying two operands

/ Division For dividing two operands and give output in double

~/ Division For dividing two operands and give output in integer

% Modulus Remainder After Integer Division

++ Increment Increase Value By 1. For E.g a++;

-- Decrement Decrease Value By 1. For E.g a–;


Increment and Decrement in dart

Operator Symbol Operator Name Description

++var Pre Increment Increase Value By 1. var = var + 1 Expression value is var+1

--var Pre Decrement Decrease Value By 1. var = var - 1 Expression value is var-1

var++ Post Increment Increase Value By 1. var = var + 1 Expression value is var

var-- Post Decrement Decrease Value By 1. var = var - 1 Expression value is var

Note: ++var increases the value of operands, whereas var++ return the
actual value of operands before the increment.
Assignment Operators

Operator Type
Description

= Assign a value to a variable

+= Adds a value to a variable

-= Reduces a value to a variable

*= Multiply value to a variable

/= Divided value by a variable


Relational Operators
Operator
Operator Name Description
Symbol
> Used to check which operand is bigger and gives result as
Greater than
boolean

< Used to check which operand is smaller and gives result as


Less than
boolean

>= Used to check which operand is bigger or equal and gives


Greater than or equal to
result as boolean

<= Used to check which operand is smaller or equal and gives


Less than or equal to
result as boolean

== Used to check operands are equal to each other and gives


Equal to
result as boolean

!= Used to check operand are not equal to each other and gives
Not equal to
result as boolean

> Used to check which operand is bigger and gives result as


Greater than
boolean
Logical Operators

Operator Type Description

&& This is ‘and’, return true if all conditions are true

|| This is ‘or’. Return true if one of the conditions is true

! This is ’not’. return false if the result is true and vice versa
Type Test Operators
Operator
Operator Name Description
Symbol
is is Gives boolean value true if the object has a specific type

is! is not Gives boolean value false if the object has a specific type
Properties of String
● codeUnits: Returns an unmodifiable list of the UTF-16 code units of this string.
● isEmpty: Returns true if this string is empty.
● isNotEmpty: Returns false if this string is empty.
● length: Returns the length of the string including space, tab, and newline characters.
Methods Of String
● toLowerCase(): Converts all characters in this string to lowercase.
● toUpperCase(): Converts all characters in this string to uppercase.
● trim(): Returns the string without any leading and trailing whitespace.
● compareTo(): Compares this object to another.
● replaceAll(): Replaces all substrings that match the specified pattern with a given value.
● split(): Splits the string at matches of the specified delimiter and returns a list of
substrings.
● toString(): Returns a string representation of this object.
● substring(): Returns the text from any position you want.
● codeUnitAt(): Returns the 16-bit UTF-16 code unit at the given index.
Types Of Condition
● If Condition
● If-Else Condition
● If-Else-If Condition
● Switch case
Ternary Operator in Dart
● The ternary operator is like if-else statement. This is a one-liner
replacement for the if-else statement. It is used to write a conditional
expression, where based on the result of a boolean condition, one of
the two values is selected.

condition ? exprIfTrue : exprIfFalse


Dart Loops
● For Loop
● For Each Loop
● While Loop
● Do While Loop
Dart Break and Continue
● The break statement is used to exit a loop. It stops the loop
immediately, and the program’s control moves outside the loop.
● The continue statement skips the current iteration of a loop. It will
bypass the statement of the loop. It does not terminate the loop but
rather continues with the next iteration.
Function In Dart
● DRY(Don’t Repeat Yourself)
● Advantage Of Function
○ Avoid Code Repetition
○ Easy to divide the complex program into smaller parts
○ Helps to write a clean code
returntype functionName(parameter1,parameter2, ...){

// function body

}
Function In Dart
● Return type: It tells you the function output type. It can be void, String, int,
double, etc. If the function doesn’t return anything, you can use void as the
return type.
● Function Name: You can name functions by almost any name. Always follow a
lowerCamelCase naming convention like void printName().
● Parameters: Parameters are the input to the function, which you can write
inside the bracket (). Always follow a lowerCamelCase naming convention for
your function parameter.
Anonymous Function In Dart
● Every function needs a name. If you remove the return type and the function
name, the function is called anonymous function.
● Syntax

(parameterList){

// statements

}
Arrow Function In Dart
● Dart has a special syntax for the function body, which is only one line. The
arrow function is represented by => symbol. It is a shorthand syntax for any
function that has only one expression.
● Syntax

returnType functionName(parameters...) => expression;


Scope In Dart
● The scope is a concept that refers to where values can be accessed or
referenced. Dart uses curly braces {} to determine the scope of variables. If you
define a variable inside curly braces, you can’t use it outside the curly braces.
● Global Scope
○ You can define a variable in the global scope to use the variable
anywhere in your program.
● Lexical Scope.
○ Dart is lexically scoped language, which means you can find the
scope of variables with the help of braces {}.
Dart Collection
● If you want to store multiple values in the same variable, you can use List. List in
dart is similar to Arrays in other programming languages. E.g. to store the names
of multiple students, you can use a List. The List is represented by Square
Braces[].
// Integer List

List<int> ages = [10, 30, 23];

// String List

List<String> names = ["Raj", "John", "Rocky"]

// Mixed List

var mixed = [10, "John", 18.8];


Types Of Lists
● Fixed Length List → The fixed-length Lists are defined with the specified length.
var list = List<int>.filled(5,0);

● Growable List [Mostly Used] → A List defined without a specified length is called Growable List.
var list1 = [210,21,22,33,44,55];
List Properties In Dart
● first: It returns the first element in the List.
● last: It returns the last element in the List.
● isEmpty: It returns true if the List is empty and false if the List is not empty.
● isNotEmpty: It returns true if the List is not empty and false if the List is empty.
● length: It returns the length of the List.
● reversed: It returns a List in reverse order.
● Single: It is used to check if the List has only one element and returns it.
List in Dart
● Adding Item To List
● Replace Range Of List
● Removing List Elements
● Loops In List
● Combine Two Or More List In Dart
● Conditions In List
● Where In List Dart
Set In Dart
● Set is a unique collection of items.
● You cannot store duplicate values in the Set.
● It is unordered, so it can be faster than lists while working with a large amount of data.
Set is useful when you need to store unique values without considering the order of
the input.
● It is represented by Curley Braces{}.
● Note: The list allows you to add duplicate items, but the Set doesn’t allow it.
Set <variable_type> variable_name = {};
Map In Dart
● In a Map, data is stored as keys and values. In Map, each key must be unique. They are
similar to HashMaps and Dictionaries in other languages.
Map<String, String> countryCapital = {

'USA': 'Washington, D.C.',

'India': 'New Delhi',

'China': 'Beijing'

};

● You can find the value of Map from its key. Here we are printing Washington, D.C. by its key,
i.e., USA.
Map Properties In Dart

Properties Work

keys To get all keys.

values To get all values.

isEmpty Return true or false.

isNotEmpty Return true or false.

length It returns the length of the Map.


Map Methods In Dart

Properties Work

keys.toList() Convert all Maps keys to List.

values.toList() Convert all Maps values to List.

containsKey(‘key’) Return true or false.

containsValue(‘value’) Return true or false.

clear() Removes all elements from the Map.

removeWhere() Removes all elements from the Map if condition is valid.


Map In Dart
● Looping Over Element Of Map
● Looping In Map Using For Each
● Remove Where In Dart Map
End

End

You might also like