Starting with Dart
1 Dart Basics
1.1 Comments
Types of Dart Comments:
-
Dart Single line Comment.
-
Dart Multiline Comment.
-
Dart Documentation Comment.
void main()
{
// This is a single comment
/*
This is a multiline comment
*/
/// This is a documentation comment.
}
1.2 Variables
To declare a variable: type variable_name;
To declare multiple variables of the same type: type variable1_name,
variable2_name, variable3_name, ....variableN_name;
Type of the variable can be among:
-
Static Variable
-
Dynamic Variable
-
Final Variables and Constants.
1.2.1 Example of Dart Variable
void main()
{
int variable1 = 10;
double variable2 = 0.2;
bool variable3 = false;
String variable4 = "0", variable5 = "Geeks for Geeks";
print(variable1);
print(variable2);
print(variable3);
print(variable4);
print(variable5);
}
void main()
{
// Declaring and initialising a variable
int variable1 = 10;
// Declaring another variable
double variable2 = 0.2; // must declare double a value or it
// will throw error
bool variable3 = false; // must declare boolean a value or it
// will throw error
// Declaring multiple variable
String variable4 = "0", variable5 = "Some text";
// Printing values of all the variables
print(variable1); // Print 10
print(variable2); // Print 0.2
print(variable3); // Print default string value
print(variable4); // Print default bool value
print(variable5); // Print Geeks for Geeks
}
1.2.2 Dynamic Type Variable in Dart
This is a special variable initialised with keyword dynamic. The variable declared with this data type can store implicitly any value during running the program. Syntax: dynamic variable_name;
void main()
{
// Assigning value to variable
dynamic variable = "Some text";
// Printing variable
print(variable);
// Reassigning the data to variable and printing it
variable = 3.14157;
print(variable);
}
If we use var instead of dynamic in the above code, it will show an error because var gets the type of the first initialized value.
1.2.3 Final and Const
These keywords are used to define constant variable in Dart i.e. once a variable is defined using these keyword then its value can’t be changed in the entire code. These keyword can be used with or without data type name. A final variable can only be set once and it is initialized when accessed. Syntax for Final: final variable_name (without datatype) or final
data_type variable_name (with datatype).
void main()
{
// Assigning value to variable without datatype
final variable1 = "Some text";
// Printing variable1
print(variable1);
// Assigning value to variable2 with datatype
final String variable2 = "Some other text";
// Printing variable2
print(variable2);
// Now, if we try to reassign the variable1 we get an error
}
A constant variable is a compile-time constant and its value must be known before the program runs. Syntax for Const: const variable_name; (without datatype) or const data_type variable_name; (with datatype).
void main()
{
// Assigning value to variable1 without datatype
const variable1 = "Some text";
// Printing variable1
print(variable1);
// Assigning value to variable2 with datatype
const variable2 = "Some text again!!";
// Printing variable variable2
print(variable2);
}
1.2.4 Null Safety in Dart
In Dart, by default a variable can’t be assigned Null value till it is defined that the variable can store Null value in it. This to avoid cases where user assign null value in Dart. To declare a variable as nullable, you append a ‘?’ to the type of the variable. The declared variable will by default store null as value.
1.3 Operators in Dart
The following are the most useful types of operators in Dart.
-
Arithmetic Operators.
-
Relational Operators.
-
Assignment Operators.
-
Logical Operators.
1.3.1 Arithmetic Operators
| Symbol | Name | Description |
|---|---|---|
| + | Addition | Use to add two operands |
| - | Subtraction | Use to subtract two operands |
| -expr | Unary Minus | It is Used to reverse the sign of the expression |
| * | Multiply | Use to multiply two operands |
| / | Division | Use to divide two operands |
| ~/ | Division | Use to divide two operands but give the result in integer |
| % | Modulus | Use to give remainder of two operands |
void main()
{
int a = 2;
int b = 3;
// Adding a and b
var c = a + b;
print("Sum ($a + $b) = $c");
// Subtracting a and b
var d = a - b;
print("Difference ($a - $b) = $d");
// Using unary minus
var e = -d;
print("Negation -($a - $b) = $e");
// Multiplication of a and b
var f = a * b;
print("Product ($a * $b) = $f");
// Division of a and b
var g = b / a;
print("Division ($b / $a) = $g");
// Using ~/ to divide a and b
var h = b ~/ a;
print("Quotient ($b ~/ $a) = $h");
// Remainder of a and b
var i = b % a;
print("Remainder ($b % $a) = $i");
}
1.3.2 Relational Operators
| Symbol | Name | Description |
|---|---|---|
| > | Greater than | Check which operand is bigger and give result as boolean expression. |
| < | Less than | Check which operand is smaller and give result as boolean expression. |
| >= | Greater than or equal to | Check which operand is greater or equal to each other and give result as boolean expression. |
| <= | less than equal to | Check which operand is less than or equal to each other and give result as boolean expression. |
| == | Equal to | Check whether the operand are equal to each other or not and give result as boolean expression. |
| != | Not Equal to | Check whether the operand are not equal to each other or not and give result as boolean expression. |
void main()
{
int a = 2;
int b = 3;
// Greater between a and b
var c = a > b;
print("a is greater than b ($a > $b) : $c");
// Smaller between a and b
var d = a < b;
print("a is smaller than b ($a < $b) : $d");
// Greater than or equal to between a and b
var e = a >= b;
print("a is greater than b ($a >= $b) : $e");
// Less than or equal to between a and b
var f = a <= b;
print("a is smaller than b ($a <= $b) : $f");
// Equality between a and b
var g = b == a;
print("a and b are equal ($b == $a) : $g");
// Unequality between a and b
var h = b != a;
print("a and b are not equal ($b != $a) : $h");
}
Note that the == operator can’t be used to check if the object is same. So, to check if the object are same we use identical() function.
1.3.3 Assignment Operators
| Symbol | Name | Description |
|---|---|---|
| = | Assignment operator | Use to assign values to the expression or variable |
| ??= | Assignment operator for null | Assign the value only if it is null. |
void main()
{
int a = 5;
int b = 7;
// Assigning value to variable c
var c = a * b;
print("assignment operator used c = $a*$b so now c = $c\n");
// Assigning value to variable d
var d;
// Value is assign as it is null
d ??= a + b;
print("Assigning value only if d is null");
print("d ??= $a+$b so d = $d \n");
// Again trying to assign value to d
d ??= a - b;
// Value is not assign as it is not null
print("Assigning value only if d is null");
print("d ??= a-b so d = $d");
print("As d was not null value was not updated");
}
There also compound assignment operators such as +=, -=, *=, /=, /=, %=, =̂, &=, and |=.
1.3.4 Logical Operators
| Symbol | Name | Description |
|---|---|---|
| && | And Operator | Use to add two conditions and if both are true than it will return true. |
| || | Or Operator | Use to add two conditions and if even one of them is true than it will return true. |
| ! | Not Operator | It is use to reverse the result. |
void main()
{
int a = 5;
int b = 7;
// Using And Operator
bool c = a > 10 && b < 10;
print(c);
// Using Or Operator
bool d = a > 10 || b < 10;
print(d);
// Using Not Operator
bool e = !(a > 10);
print(e);
}
Logical operator can only be application to boolean expression and in dart.
1.4 Standard Input/Output
1.4.1 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.
void main()
{
print("Enter your name?");
// Reading name
String? name = stdin.readLineSync(); // null safety in name string
// Printing the name
print("Hello, $name! \nWelcome!!");
}
Taking integer value as input:
// Importing dart:io file
import 'dart:io';
void main()
{
print("Enter your name?");
// Reading name
String? name = stdin.readLineSync(); // null safety in name string
// Printing the name
print("Hello, $name! \nWelcome!!");
}
// Importing dart:io file
import 'dart:io';
void main()
{
// Asking for favourite number
print("Enter your favourite number:");
// Scanning number
int n = int.parse(stdin.readLineSync()!);
// Here ! is for null safety
// Printing that number
print("Your favourite number is $n");
}
1.4.2 Standard Output in Dart
In dart, there are two ways to display output in the console:
-
Using
print()statement. -
Using
stdout.write()statement.
import 'dart:io';
void main()
{
// Printing in first way
print("Welcome!"); // printing from print statement
// Printing in second way
stdout.write("Welcome!\n"); // printing from stdout.write()
}
The print() statement brings the cursor to next line while stdout.write() does not bring the cursor to the next line, it remains in the same line.