- Dart is a programming language developed by Google.
- Dart is a compiled, statically typed, general-purpose programming language.
- Dart is a multi-paradigm, object-oriented, open-source, platform-independent, and compiled language.
- Core of Flutter Framework.
- Whitespace and Line Breaks : Dart ignores spaces, tabs, and newlines.
- Case Sensitivity : Dart is case-sensitive. That means Dart differentiates between uppercase and lowercase letters. For example "Hello" and "hello" are not the same.
- Comments : Dart supports single-line and multi-line comments. Single-line comments start with "//" and end with a newline. Multi-line comments start with "/" and end with "/".
- Statements end with a semicolon : Each line of instruction is called a statement. Every statement must end with a semicolon (;).
- Numbers : Dart supports two types of numbers : integers
int
and doublesdouble
. - Strings : Dart supports strings
String
with single''
or double""
quotes. - Lists : Dart supports lists
List
with square brackets ([]). - Maps : Dart supports maps
Map
with curly brackets ({}). - Booleans : Dart supports booleans with
true
andfalse
. - Null : Dart supports
null
.
- A variable can be declared in Dart using the
var
keyword. For example,var x = 5;
- Dart supports
type-checking
by prefixing a variable with its type. For example,int x = 5;
anddouble x = 5.0;
- Dart supports
dynamic
type. Variables declared without a static type are implicitlydynamic
type. Variables declared usingdynamic
keyword in place of thevar
keyword. For example,dynamic x = 5;
anddynamic x = "Hello";
. - The
final
andconst
keywords can be used to declare a variable as immutable. That means Dart prevents modification of the variable. For example,final x = 5;
andconst x = 5;
-
An expression is a special kind of statement that evaluates to a value. For example,
5 + 3;
is an expression ending with a semicolon. -
Every expression is composed of
operators
andoperands
.operands
are representations of data.operators
are defines how the operands will be processed to produce a result.
Operators:
-
Arithmetic Operators : Dart supports the following arithmetic operators :
+
,-
,*
,/
,%
,~/
,++
,--
. -
Comparison Operators : Dart supports the following comparison operators :
==
,!=
,>
,<
,>=
,<=
. -
Logical Operators : Dart supports the following logical operators :
&&
,||
,!
. -
Assignment Operators : Dart supports the following assignment operators :
=
,+=
,-=
,*=
,/=
,%=
,~/=
,<<=
,>>=
,&=
,|=
,^=
,??=
. -
Conditional Operators : Dart supports the following conditional operators :
??
,??=
. -
Ternary Operator : Dart supports the ternary operator.
condition ? expression1 : expression2;
-
Control Statements are used to control the flow of the program. There are three types of control statements in Dart :
- Conditional Statements
- Loop Statements
- Jump Statements
Conditional Statements:
Dart supports
if
,else if
,else
andswitch
statements.if
statementelse if
statementelse
statementswitch
statement
Loop Statements:
A loop is a block of code that can be executed repeatedly. Dart supports
for
,while
, anddo while
loops.-
for
loopfor (initialization; condition; increment/decrement) { // block of code }
-
while
loopinitialization while (condition) { // block of code increment/decrement }
-
do while
loopinitialization do { // block of code increment/decrement } while (condition);
Jump Statements:
Dart supports
break
andcontinue
statements.break
is used to exit a loop.continue
is used to skip an iteration in a loop.
- A very commonly used collection in programming is
array
. Dart representsarray
asList
. For example,List<int> list = [1, 2, 3];
- Operations on
List
:- Add -
add()
addAll()
- Insert -
insert()
insertAll()
- Remove -
remove()
removeAt()
removeLast()
removeRange()
removeWhere()
- Update -
replaceRange()
or manuallylist[index] = value
- Sort -
sort()
- Length -
length
- Clear -
clear()
- Add -
-
The
Map
is a collection of key-value pairs. Keys and values can be of any type. AMap
is a dynamic collection. Maps can be declared in two ways :-
Using Map Literals
var map = { "key1": "value1", "key2": "value2", "key3": "value3", }
-
Using the
Map
constructorvar map = new Map(); map["key1"] = "value1"; map["key2"] = "value2"; map["key3"] = "value3";
-
Operations on
Map
:addAll()
containsKey()
containsValue()
forEach()
remove()
removeWhere()
updateAll()
clear()
length
isEmpty
isNotEmpty
-
List | Map |
---|---|
Collection of Data | Collection of Data |
List use index number | Map use keys |
Dynamically increase and decrease | Dynamically increase and decrease |
List is like an array | Map is like an object or associative array |
1 element hold 1 data | 1 element hold 2 data (key, value) |
-
A
set
is a collection of unique elements. It is a dynamic collection. -
Sets can be declared in two ways :
-
Using Set Literals
var set = { "value1", "value2", "value3", }
-
Using the
Set
constructorvar set = new Set(); set.add("value1"); set.add("value2"); set.add("value3");
-
Operations on
Set
:add()
addAll()
contains()
remove()
clear()
length
isEmpty
isNotEmpty
-
- A
HashMap
is a hash table based implementation ofMap
. When you iterate through a HashMap's keys or values, you can't get the keys or values in any particular order. - Syntax :
HashMap<K, V> hashMap = HashMap();
where theK
andV
are the key and value types respectively. HashMap
is same asMap
except thatHashMap
doesn't allow duplicate keys and the order of elements is not specified.HashMap
supports all the operations ofMap
- A
HashSet
is a hash table based implementation ofSet
. When you iterate through a HashSet's elements, you can't get the elements in any particular order. - Syntax :
HashSet<K> hashSet = HashSet();
where theK
is the key type. HashSet
is same asSet
.HashSet
supports all the operations ofSet
.
-
Functions are a block of code that performs a specific task.
-
Functions can be 4 types :
-
No Arguments and No Return Type
void myFunction() { print("Hello World"); }
-
No Arguments and With Return Type
int myFunction() { return 1; }
-
With Arguments and No Return Type
void myFunction(int a, int b) { print(a + b); }
-
With Arguments and With Return Type
int myFunction(int a, int b) { return a + b; }
-
-
Dart is an object-oriented language. It supports object-oriented programming features like classes, interfaces, inheritance, polymorphism, and encapsulation.
-
A
class
is a blueprint for creating objects. It defines the properties and behaviors that an object of that class will have. -
An
object
is an instance of a class. It contains the state and behavior of the class.-
A
class
can defined by usingclass
keyword. -
A
class
can havevariables
,functions
, andobjects
.class Person { String name; int age; void printInfo() { print("Name: $name, Age: $age"); } } var person = new Person(); // Create an object of the `Person` class person.name = "John"; // Set the name of the person person.age = 30; // Set the age of the person person.printInfo(); // Print the name and age of the person
-
The
this
keyword refers to the current instance of the class. If the parameter name and the name of the class's property are the same, hence to avoid confusion, it is recommended to use thethis
keyword in Dart.
-
A
class
can be defined in another file and imported in the main file.class Person { String name; int age; void printInfo() { print("Name: $name, Age: $age"); } }
-
The
Person
class is defined in another file and imported in the main file.import 'person.dart'; void main() { Person person = new Person(); person.name = "John"; person.age = 30; person.printInfo(); }
-
A
class
can inherit the properties and methods of another class. -
Inheritance is a mechanism in which one class acquires the properties and methods of another class.
-
The
extends
keyword is used to inherit the properties and methods from another class. -
The
super
keyword is used to access the properties and methods of the parent class. -
The
super
keyword is used to call the parent class's constructor.// Parent class class Person { String name; int age; void printInfo() { print("Name: $name, Age: $age"); } } // Child class class Employee extends Person { String department; // Constructor Employee(String name, int age, String department) { this.name = name; this.age = age; this.department = department; } // Override the `printInfo()` method @override void printInfo() { super.printInfo(); print("Department: $department"); } } // Main class void main() { Employee employee = new Employee("John", 30, "IT"); employee.printInfo(); }
-