- Data Types
- Collections / Iterables
- Type Casting in Dart
- Operators
- Control flow in Dart
- Functions in Dart
Always prefer
var
to declare variables
-
Numbers --> num (int, double)
var x = 5; var y = 3.6; //num variables can hold both int and double values num x = 1; num y = 5.6; int a = 1.2; // error double b = 5;
-
Booleans --> bool (true, false)
-
Strings --> String
String singleQuote = 'allowed'; String doubleQuote = "also allowed"; String multiLine = ''' This is multi line string in dart'''; String rawString = r"It is raw string \n will not work here";
-
Lists --> (List, array in other languages)
A List is an ordered group of items.
var list = [1, 2, 3, 'Boat',]; var constantList = const [1, 2, 3]; var emptyList = []; // copy one list to another using spread operator [...] var list2 = [...list];
-
Sets --> (Set)
A Set is an unordered group of
unique
itemsvar primeSet = {1, 3, 17}; var emptySet = {}; // Creates an empty map, not a set var emptySet = <String>{}; // copy one set to another using spread operator {...} var set2 = {2, ...primeSet};
-
Maps --> (Map)
A Map is an unordered collection of
key:value
pairs, similar to a dictionary in Pythonvar map = { 1: 'dart', 2: 'flutter', }; var emptyMap = {}; // copy one map to another using spread operator {...} var map2 = {0: 'english', ...map};
var list = [1, 2, 3, 4, 5];
// Using if in a collection
var evenList = [for (var i in list) if (i % 2 == 0) i];
print(evenList); // prints: [2, 4]
// Using for in a collection
var incrementedList = [for (var i in list) i + 1];
print(incrementedList); // prints: [2, 3, 4, 5, 6]
// implicit type casting or type inference (auromatically detect type of variable)
var num = 5;
// String -> int
var one = int.parse('1'); // explicit type casting
print(one); // 1
// String -> double
var onePointOne = double.parse('1.1'); // explicit type casting
print(onePointOne); // 1.1
// int -> String
String oneAsString = 1.toString(); // explicit type casting
print(oneAsString); // '1'
// double -> int
double myDouble = 123.45;
int myInt = myDouble.toInt();
print(myInt); // 123
-
Arithmetic Operators (+, -, *, /, %, ~/, -const)
[~/ in dart ➡ // in Python]
-
Relational Operators (<, >, <=, ==, !=, etc)
-
Type Test Operators (is, is !)
String name = 'Ram'; print(name is String); // prints true
-
Bitwise Operators (&, |, ~, ^, <<, >>)
-
Assignment Operators (=, ??=)
// ??= [Assign value to x if it is null; otherwise, x stays the same] var x; x ??= 5; // Value is assigned as x is null x ??= 10; // Value is not assigned as x is not null
-
Logical Operators (&&, ||, !)
- Logical operators are only applicable to boolean expressions
- non-zero numbers are not considered as true and zero not as false
- 0 and 1 are not boolean instead int
-
Conditional Operators / Ternary operators (?: and ??)
- (condition ? expr1 : expr2)
If condition is true, evaluates expr1 (and returns its value); otherwise, evaluates and returns the value of expr2
- (expr1 ?? expr2)
If expr1 is null, returns expr2; otherwise, evaluates and returns the value of expr1
int a = 5, b = 8; a > b ? print(a) : print(b); String? name; // made name nullable using ? String nameToUse = name ?? 'Guest'; // Null coalescing operator
- (condition ? expr1 : expr2)
-
Cascade Notation Operator (..)
used to perform multiple method calls or multiple operations on same object
class MyClass { void methodA() { print('Method A'); } void methodB() { print('Method B'); } } void main() { MyClass myObject = MyClass(); // without using cascade notation myObject.methodA(); myObject.methodB(); // Using cascade notation myObject ..methodA() ..methodB(); } // output Method A Method B
-
if-else
-
switch-case
var animal = 'Dog'; switch (animal) { case 'Dog': print('You have a dog.'); break; case 'Bird': print('You have a bird.'); break; default: print('Animal not recognized.'); }
-
for loop (for, for-in, for-each)
// for loop for (var i = 0; i < 5; i++) { print(i); // 0 1 2 3 4 } // for-in loop var word = ['f', 'o', 'r', 'i', 'n']; for (var letters in word) { print(letters); // f o r i n } // for-each loop word.forEach(print); // f o r i n
-
while loop
when we don't know the number of iterations
-
do-while loop
executes once then checks the condition
-
break and continue
-
code reusability
-
return type is optional,
dynamic
if not specified -
datatype of parameters is optional,
dynamic
if not specified -
arguments: visible in fn call (actual parameters)
-
parameters: visible in fn definition (formal parameters)
-
Parameter types:
- positional:
(int x, int y)
called like
fn(5, 6)
- named:
({int x, int y})
, { } indicates named parameterscalled like
fn(x: 5, y: 6)
- default:
(x, [y = 0])
, [ ] indicates default parameterscalled like
fn(5)
- optional:
- positional:
([int? x, int? y])
called like
fn(5, 6)
- named:
({x, int? y})
called like
fn(x: 5, y: 6)
- default:
(int? x, [y = 10])
called like
fn(5)
- positional:
- positional:
-
Anonymous fn
/ Lambda fn / fat arrow / single line fnvar sum = (int x, int y) => x + y; print(sum(5, 6)); // 11
-
Function as first-class objects
as they can be assigned to variables or passed as arguments to other functions
void main() { var sum = (int x, int y) => x + y; print(sum(5, 6)); // 11 var myFn = sum; print(myFn(5, 6)); // 11 }