Dart
SE SSION-2
WWW.SINET.IN
Dart Data Types
The data types are the most important fundamental features of programing language. In Dart, the data type of the variable is defined by
its value. The variables are used to store values and reserve the memory location. The data-type specifies what type of value will be
stored by the variable. Each variable has its data-type. The Dart is a static type of language, which means that the variables cannot
modify.
Dart supports the following built-in Data types.
o Number
o Strings
o Boolean
o Lists
o Maps
o Runes
o Symbols
WWW.SINET.IN
Number
The Darts Number is used to store the numeric values. The number can be two types - integer and double.
o Integer - Integer values represent the whole number or non-fractional values. An integer data type
represents the 64-bit non-decimal numbers between -263 to 263. A variable can store an unsigned or
signed integer value. The example is given below -
int marks = 80;
o Double - Double value represents the 64-bit of information (double-precision) for floating number or
number with the large decimal points. The double keyword is used to declare the double type variable.
double pi = 3.14;
WWW.SINET.IN
Strings
A string is the sequence of the character. If we store the data like - name, address, special character, etc. It is
signified by using either single quotes or double quotes. A Dart string is a sequence of UTF-16 code units.
var msg = "Welcome to SINET";
Dart Boolean
The Boolean type represents the two values - true and false. The bool keyword uses to denote Boolean Type. The
numeric values 1 and 0 cannot be used to represent the true or false value.
bool isValid = true;
Dart Lists
In Dart, The list is a collection of the ordered objects (value). The concept of list is similar to an array. An array is
defined as a collection of the multiple elements in a single variable. The elements in the list are separated by the
comma enclosed in the square bracket[]. The sample list is given below.
var list = [1,2,3]
WWW.SINET.IN
Dart Maps
The maps type is used to store values in key-value pairs. Each key is associated with its value. The key and
value can be any type. In Map, the key must be unique, but a value can occur multiple times. The Map is
defined by using curly braces ({}), and comma separates each pair.
var student = {'name': 'Joseph', 'age':25, 'Branch': 'Computer Science'}
Dart Runes
As we know that, the strings are the sequence of Unicode UTF-16 code units. Unicode is a technique which
is used to describe a unique numeric value for each digit, letter, and symbol. Since Dart Runes are the
special string of Unicode UTF-32 units. It is used to represent the special syntax.
WWW.SINET.IN
For example - The special heart character is equivalent to Unicode code \u2665, where \u means Unicode, and
the numbers are hexadecimal integer. If the hex value is less or greater than 4 digits, it places in a curly bracket
({}). For example - An emoji is represented as \u{1f600}. The example is given below.
void main(){
var heart_symbol = '\u2665';
var laugh_symbol = '\u{1f600}';
print(heart_symbol);
print(laugh_symbol);
}
Output:
WWW.SINET.IN
Dart Symbols
The Dart Symbols are the objects which are used to refer an operator or identifier that declare in a Dart
program. It is commonly used in APIs that refers to identifiers by name because an identifier name can
changes but not identifier symbols.
Dart Dynamic Type
Dart is an optionally typed language. If the variable type is not specified explicitly, then the variable type is
dynamic. The dynamic keyword is used for type annotation explicitly.
WWW.SINET.IN
void main() {
// declaring variables
String name = "John";
String address = "USA";
num age = 20; // used to store any types of numbers
num height = 5.9;
bool isMarried = false;
// printing variables value
print("Name is $name");
print("Address is $address");
print("Age is $age");
print("Height is $height");
print("Married Status is $isMarried");
}
WWW.SINET.IN
Rules For Creating Variables In Dart
•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 a number.
•Keywords are not allowed to be used 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.
WWW.SINET.IN
Variable
Variable is used to store the value and refer the memory location in
computer memory. When we create a variable, the Dart compiler
allocates some space in memory. The size of the memory block of
memory is depended upon the type of variable. To create a variable, we
should follow certain rules. Here is an example of a creating variable and
assigning value to it.
var name = 'Devansh';
WWW.SINET.IN
How to declare variable in Dart
We need to declare a variable before using it in a program. In Dart, The var keyword is used to declare a
variable. The Dart compiler automatically knows the type of data based on the assigned to the variable
because Dart is an infer type language. The syntax is given below.
Syntax -
var <variable_name> = <value>;
or
var <variable_name>;
Example -
var name = 'Andrew' ;
In the above example, the variable name has allocated some space in the memory. The semicolon(;) is
necessary to use because it separates program statement to another.
WWW.SINET.IN
Type Annotations
As we had pointed out, The Dart is an infer language but it also provides a type annotation. While declaring the
variable, it suggests the type of the value that variable can store. In the type annotation, we add the data type as
a prefix before the variable's name that ensures that the variable can store specific data type. The syntax is given
below.
Syntax -
<type> <variable_name>;
or
<type> <name> = <expression>;
Example -
int age;
String msg = "Welcome ";
WWW.SINET.IN
Declaring the variable with Multiple Values
int i,j,k;
Default Value
While declaring the variable without initializing the value then the Dart compiler provides default value
(Null) to the variable. Even the numeric type variables are initially assigned with the null value. Let's
consider the following example.
int count;
assert(count == null);
WWW.SINET.IN
Final
When we do not want to change a variable in the future then we use final and const. It can be used in
place of var or in addition to a type. A final variable can be set only one time where the variable is a
compile-time constant. The example of creating a final variable is given below.
Example -
final name = 'Ricky'; // final variable without type annotation.
final String msg = 'How are you?'; // final variable with type annotation.
If we try to change these values then it will throw an error.
name = 'Roger'; // Error: Final variable can't be changed.
WWW.SINET.IN
const
The const is used to create compile-time constants. We can declare a value to compile-time constant such
as number, string literal, a const variable, etc.
const a = 1000;
The const keyword is also used to create a constant value that cannot be changed after its creation.
var f = const[];
If we try to change it, then it will throw an error.
f = [12]; //Error, The const variable cannot be change
WWW.SINET.IN
Operators
An operator is a symbol that is used to manipulating the values or performs operations on its operand. The given expression: 5+4, in this
expression, 5 and 4 are operands and "+" is the operator.
Types of Operators
Dart supports the following types of operators.
o Arithmetic Operators
o Assignment Operators
o Relational Operators
o Type test Operators
o Logical Operators
o Bitwise Operator
o Conditional Operators
o Casecade notation(..) Operators
WWW.SINET.IN
Arithmetic Operators
Sr. Operator Name Description Example
1. Addition(+) It adds the left operand to the right operand. a+b will return 30
2. Subtraction(-) It subtracts the right operand from the left operand. a-b will return 10
3 Divide(/) It divides the first operand by the second operand and returns quotient. a/b will return 2.0
4. Multiplication(*) It multiplies the one operand to another operand. a*b will return 200
5. Modulus(%) It returns a reminder after dividing one operand to another. a%b will return 0
6. Division(~/) It divides the first operand by the second operand and returns integer quotient. a/b will return 2
7. Unary Minus(-expr) It is used with a single operand changes the sign of it. -(a-b) will return -10
WWW.SINET.IN
void main(){
print("Example of Assignment operators");
var n1 = 10;
var n2 = 5;
print("n1+n2 = ${n1+n2}");
print("n1-n2 = ${n1-n2}");
print("n1*n2 = ${n1*n2}");
print("n1/=n2 = ${n1/n2}");
print("n1%n2 = ${n1%n2}");
WWW.SINET.IN
Standard Input in Dart:
In Dart programming language, you can take standard input from the user through the console via the use
of .readLineSync() function. To take input from the console you need to import a library, named dart:io from libraries of Dart.
About Stdin Class: This class allows the user to read data from standard input in both synchronous and asynchronous ways. The
method readLineSync() is one of the methods used to take input from the user.
String user input
import 'dart:io';
void main() {
print("Enter name:");
String? name = stdin.readLineSync();
print("The entered name is ${name}");
WWW.SINET.IN
Integer User input
import 'dart:io';
void main() {
print("Enter number:");
int? number = int.parse(stdin.readLineSync()!);
print("The entered number is ${number}");
}
WWW.SINET.IN
Floating user input
import 'dart:io';
void main() {
print("Enter a floating number:");
double number = double.parse(stdin.readLineSync()!);
print("The entered num is $number");
}
WWW.SINET.IN
import 'dart:io';
import 'dart:io';
void main()
void main()
{
print("Enter two no's");
{ var n1 = stdin.readLineSync();
var n2 = stdin.readLineSync();
var s = int.parse(n1!) +
print("Enter first number"); num.parse(n2!);
print('sum=$s’);
/* print("Enter two no's");
int? n1 = int.parse(stdin.readLineSync()!); var no1 =
int.parse(stdin.readLineSync()!);
var no2 =
print("Enter second number"); int.parse(stdin.readLineSync()!);
var sum = no1 + no2;
int? n2 = int.parse(stdin.readLineSync()!); print('sum of $no1+$no2=$sum');
print('sum of $no1+$no2=${no1 +
no2}');*/
// Adding them and printing them /*num a = 10;
a = 23.6;
print(a);*/}
int sum = n1 + n2; }
print("Sum is $sum");
WWW.SINET.IN
Unary Operators (post and pre)
Sr. Operator Name Description Example
1. ++(Prefix) It increment the value of operand. ++x
2. ++(Postfix) It returns the actual value of operand before increment. x++
3. --(Prefix) It decrement the value of the operand. --x
4. --(Postfix) It returns the actual value of operand before decrement. x--
WWW.SINET.IN
void main() {
var x = 30;
print(x++); //The postfix value
var y = 25;
print(++y); //The prefix value,
var z = 10;
print(--z); //The prefix value
var u = 12;
print(u--); //The postfix
}
WWW.SINET.IN
Assignment Operator
Operators Name Description
= (Assignment Operator) It assigns the right expression to the left operand.
+=(Add and Assign) It adds right operand value to the left operand and resultant assign back to the left
operand. For example - a+=b → a = a+b → 30
-=(Subtract and Assign) It subtracts right operand value from left operand and resultant assign back to the left
operand. For example - a-=b → a = a-b → 10
*=(Multiply and Assign) It multiplies the operands and resultant assign back to the left operand. For example -
a*=b → a = a*b → 200
/=(Divide and Assign) It divides the left operand value by the right operand and resultant assign back to the
left operand. For example - a%=b → a = a%b → 2.0
~/=(Divide and Assign) It divides the left operand value by the right operand and integer remainder quotient
back to the left operand. For example - a%=b → a = a%b → 2
%=(Mod and Assign) It divides the left operand value by the right operand and remainder assign back to the
left operand. For example - a%=b → a = a%b → 0
<<=(Left shift AND assign) The expression a<<=3 is equal to a = a<<3
>>=(Right shift AND assign) The expression a>>=3 is equal to a = a>>3
&=(Bitwise AND assign) The expression a&=3 is equal to a = a&3
^=(Bitwise exclusive OR and assign) The expression a^=3 is equal to a = a^3
|=(Bitwise inclusive OR and assign) The expression a|=3 is equal to a = a|3
WWW.SINET.IN
void main(){
print("Example of Assignment operators");
var n1 = 10;
var n2 = 5;
n1+=n2;
print("n1+=n2 = ${n1}");
n1-=n2;
print("n1-=n2 = ${n1}");
n1*=n2;
print("n1*=n2 = ${n1}");
n1~/=n2;
print("n1~/=n2 = ${n1}");
n1%=n2;
print("n1%=n2 = ${n1}");
}
WWW.SINET.IN
Relational Operator
Sr. Operator Description
1. >(greater than) a>b will return TRUE.
2. <(less than) a<b will return FALSE.
3. >=(greater than or equal to) a>=b will return TRUE.
4. <=(less than or equal to) a<=b will return FALSE.
5. ==(is equal to) a==b will return FALSE.
6. !=(not equal to) a!=b will return TRUE.
WWW.SINET.IN
void main() {
var a = 30;
var b = 20;
print("The example of Relational Operator");
var res = a>b;
print("a is greater than b: "+res. toString());
var res0 = a<b;
print("a is less than b: "+res0. toString());
var res1 = a>=b;
print("a is greater than or equal to b: "+res1. toString());
var res2 = a<=b;
print("a is less than and equal to b: "+res2. toString());
var res3 = a!= b;
print("a is not equal to b: "+res3. toString());
var res4 = a==b;
print("a is equal to b: "+res4. toString());
}
WWW.SINET.IN
Type Test Operators
The Type Test Operators are used to testing the types of expressions at runtime
Sr. Operator Description
1. as It is used for typecast.
2. is It returns TRUE if the object has specified type.
3. is! It returns TRUE if the object has not specified type.
WWW.SINET.IN
void main()
var num = 10;
var name = "SINET";
print(num is int);
print(name is! String );
WWW.SINET.IN
Logical Operators
The Logical Operators are used to evaluate the expressions and make the decision.
Sr. Operator Description
1. &&(Logical AND) It returns if all expressions are true.
2. ||(Logical OR) It returns TRUE if any expression is true.
3. !(Logical NOT) It returns the complement of expression.
WWW.SINET.IN
void main(){
bool bool_val1 = true, bool_val2 = false;
print("Example of the logical operators");
var val1 = bool_val1 && bool_val2;
print(val1);
var val2 = bool_val1 || bool_val2;
print(val2);
var val3 = !(bool_val1 || bool_val2);
print(val3);
}
WWW.SINET.IN
void main(){
int userid = 123;
int userpin = 456;
// Printing Info
print((userid == 123) && (userpin== 456)); // print true
print((userid == 1213) && (userpin== 456)); // print false.
print((userid == 123) || (userpin== 456)); // print true.
print((userid == 1213) || (userpin== 456)); // print true
print((userid == 123) != (userpin== 456));//print false
WWW.SINET.IN
Bitwise Operators
The Bitwise operators perform operation bit by bit on the value of the two operands.
a=7
b=6
then binary(a) = 0111
binary(b) = 0011
Hence a & b = 0011, a|b = 0111 and a^b = 0100
WWW.SINET.IN
Sr. Operators Description
1. &(Binary AND) It returns 1 if both bits are 1.
2. |(Binary OR) It returns 1 if any of bit is 1.
3. ^(Binary XOR) It returns 1 if both bits are different.
4. ~(Ones Compliment) It returns the reverse of the bit. If bit is 0 then the compliment will be 1.
5. <<(Shift left) The value of left operand moves left by the number of bits present in the right
operand.
6. >>(Shift right) The value of right operand moves left by the number of bits present in the left
operand.
WWW.SINET.IN
void main(){
print("Example of Bitwise operators");
var a = 25;
var b = 20;
var c = 0;
// Bitwise AND Operator
print("a & b = ${a&b}");
// Bitwise OR Operator
print("a | b = ${a|b}");
// Bitwise XOR
print("a ^ b = ${a^b}");
// Complement Operator
print("~a = ${(~a)}");
// Binary left shift Operator
c = a <<2;
print("c<<1= ${c}");
// Binary right shift Operator
c = a >>2;
print("c>>1= ${c}");
}
WWW.SINET.IN
Cascade notation Operators
The Cascade notation Operators (..) is used to evaluate a series of operation on the same object. It is an
identical as the method chaining that avoids several of steps, and we don't need store results in temporary
variables.
Class obj=new Class();
obj..a=10;
..show();
WWW.SINET.IN
END
WWW.SINET.IN