- 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
intand doublesdouble. - Strings : Dart supports strings
Stringwith single''or double""quotes. - Lists : Dart supports lists
Listwith square brackets ([]). - Maps : Dart supports maps
Mapwith curly brackets ({}). - Booleans : Dart supports booleans with
trueandfalse. - Null : Dart supports
null.
- A variable can be declared in Dart using the
varkeyword. For example,var x = 5; - Dart supports
type-checkingby prefixing a variable with its type. For example,int x = 5;anddouble x = 5.0; - Dart supports
dynamictype. Variables declared without a static type are implicitlydynamictype. Variables declared usingdynamickeyword in place of thevarkeyword. For example,dynamic x = 5;anddynamic x = "Hello";. - The
finalandconstkeywords 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
operatorsandoperands.operandsare representations of data.operatorsare 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,elseandswitchstatements.ifstatementelse ifstatementelsestatementswitchstatement
Loop Statements:
A loop is a block of code that can be executed repeatedly. Dart supports
for,while, anddo whileloops.-
forloopfor (initialization; condition; increment/decrement) { // block of code }
-
whileloopinitialization while (condition) { // block of code increment/decrement }
-
do whileloopinitialization do { // block of code increment/decrement } while (condition);
Jump Statements:
Dart supports
breakandcontinuestatements.breakis used to exit a loop.continueis used to skip an iteration in a loop.
- A very commonly used collection in programming is
array. Dart representsarrayasList. 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
Mapis a collection of key-value pairs. Keys and values can be of any type. AMapis a dynamic collection. Maps can be declared in two ways :-
Using Map Literals
var map = { "key1": "value1", "key2": "value2", "key3": "value3", }
-
Using the
Mapconstructorvar map = new Map(); map["key1"] = "value1"; map["key2"] = "value2"; map["key3"] = "value3";
-
Operations on
Map:addAll()containsKey()containsValue()forEach()remove()removeWhere()updateAll()clear()lengthisEmptyisNotEmpty
-
| 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
setis 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
Setconstructorvar set = new Set(); set.add("value1"); set.add("value2"); set.add("value3");
-
Operations on
Set:add()addAll()contains()remove()clear()lengthisEmptyisNotEmpty
-
- A
HashMapis 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 theKandVare the key and value types respectively. HashMapis same asMapexcept thatHashMapdoesn't allow duplicate keys and the order of elements is not specified.HashMapsupports all the operations ofMap
- A
HashSetis 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 theKis the key type. HashSetis same asSet.HashSetsupports 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
classis a blueprint for creating objects. It defines the properties and behaviors that an object of that class will have. -
An
objectis an instance of a class. It contains the state and behavior of the class.-
A
classcan defined by usingclasskeyword. -
A
classcan 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
thiskeyword 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 thethiskeyword in Dart.
-
A
classcan 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
Personclass 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
classcan 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
extendskeyword is used to inherit the properties and methods from another class. -
The
superkeyword is used to access the properties and methods of the parent class. -
The
superkeyword 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(); }
-