A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.
The logical representation of a list in Dart is given below −
test_list − is the identifier that references the collection.
The list contains in it the values 12, 13, and 14. The memory blocks holding these values are known as elements.
Each element in the List is identified by a unique number called the index. The index starts from zero and extends up to n-1 where n is the total number of elements in the List. The index is also referred to as the subscript.
Lists can be classified as −
Fixed Length List
Growable List
Let us now discuss these two types of lists in detail.
Fixed Length List
A fixed length list’s length cannot change at runtime. The syntax for creating a fixed length list is as given below −
Step 1 − Declaring a list
The syntax for declaring a fixed length list is given below −
var list_name = new List(initial_size)
The above syntax creates a list of the specified size. The list cannot grow or shrink at runtime. Any attempt to resize the list will result in an exception.
Step 2 − Initializing a list
The syntax for initializing a list is as given below −
lst_name[index] = value;
Example
void main() {
var lst = new List(3);
lst[0] = 12;
lst[1] = 13;
lst[2] = 11;
print(lst);
}
It will produce the following output −
[12, 13, 11]
Growable List
A growable list’s length can change at run-time. The syntax for declaring and initializing a growable list is as given below −
Step 1 − Declaring a List
var list_name = [val1,val2,val3]
--- creates a list containing the specified values
OR
var list_name = new List()
--- creates a list of size zero
Step 2 − Initializing a List
The index / subscript is used to reference the element that should be populated with a value. The syntax for initializing a list is as given below −
list_name[index] = value;
Example
The following example shows how to create a list of 3 elements.Live Demo
void main() {
var num_list = [1,2,3];
print(num_list);
}
It will produce the following output −
[1, 2, 3]
Example
The following example creates a zero-length list using the empty List() constructor. The add() function in the List class is used to dynamically add elements to the list.
void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print(lst);
}
It will produce the following output −
[12, 13]
List Properties
The following table lists some commonly used properties of the List class in the dart:core library.
Sr.No
Methods & Description
1
firstReturns the first element in the list.
2
isEmptyReturns true if the collection has no elements.
3
isNotEmptyReturns true if the collection has at least one element.
4
lengthReturns the size of the list.
5
lastReturns the last element in the list.
6
reversedReturns an iterable object containing the lists values in the reverse order.
7
SingleChecks if the list has only one element and returns it.
Dart provides an inbuilt support for the Boolean data type. The Boolean data type in DART supports only two values – true and false. The keyword bool is used to represent a Boolean literal in DART.
The syntax for declaring a Boolean variable in DART is as given below −
Unlike JavaScript, the Boolean data type recognizes only the literal true as true. Any other value is considered as false. Consider the following example −
var str = 'abc';
if(str) {
print('String is not empty');
} else {
print('Empty String');
}
The above snippet, if run in JavaScript, will print the message ‘String is not empty’ as the if construct will return true if the string is not empty.
However, in Dart, str is converted to false as str != true. Hence the snippet will print the message ‘Empty String’ (when run in unchecked mode).
Example
The above snippet if run in checked mode will throw an exception. The same is illustrated below −
void main() {
var str = 'abc';
if(str) {
print('String is not empty');
} else {
print('Empty String');
}
}
It will produce the following output, in Checked Mode −
Unhandled exception:
type 'String' is not a subtype of type 'bool' of 'boolean expression' where
String is from dart:core
bool is from dart:core
#0 main (file:///D:/Demos/Boolean.dart:5:6)
#1 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)
It will produce the following output, in Unchecked Mode −
Empty String
Note − The WebStorm IDE runs in checked mode, by default.
The String data type represents a sequence of characters. A Dart string is a sequence of UTF 16 code units.
String values in Dart can be represented using either single or double or triple quotes. Single line strings are represented using single or double quotes. Triple quotes are used to represent multi-line strings.
The syntax of representing string values in Dart is as given below −
Syntax
String variable_name = 'value'
OR
String variable_name = ''value''
OR
String variable_name = '''line1
line2'''
OR
String variable_name= ''''''line1
line2''''''
The following example illustrates the use of String data type in Dart.
void main() {
String str1 = 'this is a single line string';
String str2 = "this is a single line string";
String str3 = '''this is a multiline line string''';
String str4 = """this is a multiline line string""";
print(str1);
print(str2);
print(str3);
print(str4);
}
It will produce the following Output −
this is a single line string
this is a single line string
this is a multiline line string
this is a multiline line string
Strings are immutable. However, strings can be subjected to various operations and the resultant string can be a stored as a new value.
String Interpolation
The process of creating a new string by appending a value to a static string is termed as concatenation or interpolation. In other words, it is the process of adding a string to another string.
The operator plus (+) is a commonly used mechanism to concatenate / interpolate strings.
int − Integer of arbitrary size. The int data type is used to represent whole numbers.
double − 64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard. The double data type is used to represent fractional numbers
The num type is inherited by the int and double types. The dart core library allows numerous operations on numeric values.
The syntax for declaring a number is as given below −
int var_name; // declares an integer variable
double var_name; // declares a double variable
Example
void main() {
// declare an integer
int num1 = 10;
// declare a double value
double num2 = 10.50;
// print the values
print(num1);
print(num2);
}
It will produce the following output −
10
10.5
Note − The Dart VM will throw an exception if fractional values are assigned to integer variables.
Parsing
The parse() static function allows parsing a string containing numeric literal into a number. The following illustration demonstrates the same −
The above code will result in the following output −
12
10.91
The parse function throws a FormatException if it is passed any value other than numerals. The following code shows how to pass an alpha-numeric value to the parse() function.
A conditional/decision-making construct evaluates a condition before the instructions are executed.
Conditional constructs in Dart are classified in the following table.
Sr.No
Statement & Description
1
if statementAn if statement consists of a Boolean expression followed by one or more statements.
2
If…Else StatementAn if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if block evaluates to false.
3
else…if LadderThe else…if ladder is useful to test multiple conditions. Following is the syntax of the same.
4
switch…case StatementThe switch statement evaluates an expression, matches the expression’s value to a case clause and executes the statements associated with that case.
At times, certain instructions require repeated execution. Loops are an ideal way to do the same. A loop represents a set of instructions that must be repeated. In a loop’s context, a repetition is termed as an iteration.
The following figure illustrates the classification of loops −
Let’s start the discussion with Definite Loops. A loop whose number of iterations are definite/fixed is termed as a definite loop.
Sr.No
Loop & Description
1
for loopThe for loop is an implementation of a definite loop. The for loop executes the code block for a specified number of times. It can be used to iterate over a fixed set of values, such as an array
2
for…in LoopThe for…in loop is used to loop through an object’s properties.
Moving on, let’s now discuss the indefinite loops. An indefinite loop is used when the number of iterations in a loop is indeterminate or unknown. Indefinite loops can be implemented using −
Sr.No
Loop & Description
1
while LoopThe while loop executes the instructions each time the condition specified evaluates to true. In other words, the loop evaluates the condition before the block of code is executed.
2
do…while LoopThe do…while loop is similar to the while loop except that the do…while loop doesn’t evaluate the condition for the first time the loop executes.
Let us now move on and discuss the Loop Control Statements of Dart.
Sr.No
Control Statement & Description
1
break StatementThe break statement is used to take the control out of a construct. Using break in a loop causes the program to exit the loop. Following is an example of the break statement.
2
continue StatementThe continue statement skips the subsequent statements in the current iteration and takes the control back to the beginning of the loop.
Using Labels to Control the Flow
A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code. A label can be used with break and continue to control the flow more precisely.
Line breaks are not allowed between the ‘continue’ or ‘break’ statement and its label name. Also, there should not be any other statement in between a label name and an associated loop.
Example: Label with Break
void main() {
outerloop: // This is the label name
for (var i = 0; i < 5; i++) {
print("Innerloop: ${i}");
innerloop:
for (var j = 0; j < 5; j++) {
if (j > 3 ) break ;
// Quit the innermost loop
if (i == 2) break innerloop;
// Do the same thing
if (i == 4) break outerloop;
// Quit the outer loop
print("Innerloop: ${j}");
}
}
}
The following output is displayed on successful execution of the above code.
An expression is a special kind of statement that evaluates to a value. Every expression is composed of −
Operands − Represents the data
Operator − Defines how the operands will be processed to produce a value.
Consider the following expression – “2 + 3”. In this expression, 2 and 3 are operands and the symbol “+” (plus) is the operator.
In this chapter, we will discuss the operators that are available in Dart.
Arithmetic Operators
Equality and Relational Operators
Type test Operators
Bitwise Operators
Assignment Operators
Logical Operators
Arithmetic Operators
The following table shows the arithmetic operators supported by Dart.
Sr.No
Operators & Meaning
1
+Add
2
−Subtract
3
-exprUnary minus, also known as negation (reverse the sign of the expression)
4
*Multiply
5
/Divide
6
~/Divide, returning an integer result
7
%Get the remainder of an integer division (modulo)
8
++Increment
9
—Decrement
Equality and Relational Operators
Relational Operators tests or defines the kind of relationship between two entities. Relational operators return a Boolean value i.e. true/ false.
Assume the value of A is 10 and B is 20.
Operator
Description
Example
>
Greater than
(A > B) is False
<
Lesser than
(A < B) is True
>=
Greater than or equal to
(A >= B) is False
<=
Lesser than or equal to
(A <= B) is True
==
Equality
(A==B) is False
!=
Not equal
(A!=B) is True
Type test Operators
These operators are handy for checking types at runtime.
Operator
Meaning
is
True if the object has the specified type
is!
False if the object has the specified type
Bitwise Operators
The following table lists the bitwise operators available in Dart and their role −
Operator
Description
Example
Bitwise AND
a & b
Returns a one in each bit position for which the corresponding bits of both operands are ones.
Bitwise OR
a | b
Returns a one in each bit position for which the corresponding bits of either or both operands are ones.
Bitwise XOR
a ^ b
Returns a one in each bit position for which the corresponding bits of either but not both operands are ones.
Bitwise NOT
~ a
Inverts the bits of its operand.
Left shift
a ≪ b
Shifts a in binary representation b (< 32) bits to the left, shifting in zeroes from the right.
Signpropagating right shift
a ≫ b
Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.
Assignment Operators
The following table lists the assignment operators available in Dart.
Sr.No
Operator & Description
1
=(Simple Assignment )Assigns values from the right side operand to the left side operandEx:C = A + B will assign the value of A + B into C
2
??=Assign the value only if the variable is null
3
+=(Add and Assignment)It adds the right operand to the left operand and assigns the result to the left operand.Ex: C += A is equivalent to C = C + A
4
─=(Subtract and Assignment)It subtracts the right operand from the left operand and assigns the result to the left operand.Ex: C -= A is equivalent to C = C – A
5
*=(Multiply and Assignment)It multiplies the right operand with the left operand and assigns the result to the left operand.Ex: C *= A is equivalent to C = C * A
6
/=(Divide and Assignment)It divides the left operand with the right operand and assigns the result to the left operand.
Note − Same logic applies to Bitwise operators, so they will become ≪=, ≫=, ≫=, ≫=, |= and ^=.
Logical Operators
Logical operators are used to combine two or more conditions. Logical operators return a Boolean value. Assume the value of variable A is 10 and B is 20.
Operator
Description
Example
&&
And − The operator returns true only if all the expressions specified return true
(A > 10 && B > 10) is False.
||
OR − The operator returns true if at least one of the expressions specified return true
(A > 10 || B > 10) is True.
!
NOT − The operator returns the inverse of the expression’s result. For E.g.: !(7>5) returns false
!(A > 10) is True.
Conditional Expressions
Dart has two operators that let you evaluate expressions that might otherwise require ifelse statements −
condition ? expr1 : expr2
If condition is true, then the expression evaluates expr1 (and returns its value); otherwise, it evaluates and returns the value of expr2.
expr1 ?? expr2
If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2
Example
The following example shows how you can use conditional expression in Dart −
void main() {
var a = 10;
var res = a > 12 ? "value greater than 10":"value lesser than or equal to 10";
print(res);
}
It will produce the following output −
value lesser than or equal to 10
Example
Let’s take another example −
void main() {
var a = null;
var b = 12;
var res = a ?? b;
print(res);
}
A variable is “a named space in the memory” that stores values. In other words, it acts a container for values in a program. Variable names are called identifiers. Following are the naming rules for an identifier −
Identifiers cannot be keywords.
Identifiers can contain alphabets and numbers.
Identifiers cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign.
Variable names cannot begin with a number.
Type Syntax
A variable must be declared before it is used. Dart uses the var keyword to achieve the same. The syntax for declaring a variable is as given below −
var name = 'Smith';
All variables in dart store a reference to the value rather than containing the value. The variable called name contains a reference to a String object with a value of “Smith”.
Dart supports type-checking by prefixing the variable name with the data type. Type-checking ensures that a variable holds only data specific to a data type. The syntax for the same is given below −
String name = 'Smith';
int num = 10;
Consider the following example −
void main() {
String name = 1;
}
The above snippet will result in a warning since the value assigned to the variable doesn’t match the variable’s data type.
Output
Warning: A value of type 'String' cannot be assigned to a variable of type 'int'
All uninitialized variables have an initial value of null. This is because Dart considers all values as objects. The following example illustrates the same −
void main() {
int num;
print(num);
}
Output
Null
The dynamic keyword
Variables declared without a static type are implicitly declared as dynamic. Variables can be also declared using the dynamic keyword in place of the var keyword.
The following example illustrates the same.
void main() {
dynamic x = "tom";
print(x);
}
Output
tom
Final and Const
The final and const keyword are used to declare constants. Dart prevents modifying the values of a variable declared using the final or const keyword. These keywords can be used in conjunction with the variable’s data type or instead of the var keyword.
The const keyword is used to represent a compile-time constant. Variables declared using the const keyword are implicitly final.
Syntax: final Keyword
final variable_name
OR
final data_type variable_name
Syntax: const Keyword
const variable_name
OR
const data_type variable_name
Example – final Keyword
void main() {
final val1 = 12;
print(val1);
}
Output
12
Example – const Keyword
void main() {
const pi = 3.14;
const area = pi*12*12;
print("The output is ${area}");
}
The above example declares two constants, pi and area, using the const keyword. The area variable’s value is a compile-time constant.
Output
The output is 452.15999999999997
Note − Only const variables can be used to compute a compile time constant. Compile-time constants are constants whose values will be determined at compile time
Example
Dart throws an exception if an attempt is made to modify variables declared with the final or const keyword. The example given below illustrates the same −
One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language.
The Dart language supports the following types−
Numbers
Strings
Booleans
Lists
Maps
Numbers
Numbers in Dart are used to represent numeric literals. The Number Dart come in two flavours −
Integer − Integer values represent non-fractional values, i.e., numeric values without a decimal point. For example, the value “10” is an integer. Integer literals are represented using the int keyword.
Double − Dart also supports fractional numeric values i.e. values with decimal points. The Double data type in Dart represents a 64-bit (double-precision) floating-point number. For example, the value “10.10”. The keyword double is used to represent floating point literals.
Strings
Strings represent a sequence of characters. For instance, if you were to store some data like name, address etc. the string data type should be used. A Dart string is a sequence of UTF-16 code units. Runes are used to represent a sequence of UTF-32 code units.
The keyword String is used to represent string literals. String values are embedded in either single or double quotes.
Boolean
The Boolean data type represents Boolean values true and false. Dart uses the bool keyword to represent a Boolean value.
List and Map
The data types list and map are used to represent a collection of objects. A List is an ordered group of objects. The List data type in Dart is synonymous to the concept of an array in other programming languages. The Map data type represents a set of values as key-value pairs. The dart: core library enables creation and manipulation of these collections through the predefined List and Map classes respectively.
The Dynamic Type
Dart is an optionally typed language. If the type of a variable is not explicitly specified, the variable’s type is dynamic. The dynamic keyword can also be used as a type annotation explicitly.
Syntax defines a set of rules for writing programs. Every language specification defines its own syntax. A Dart program is composed of −
Variables and Operators
Classes
Functions
Expressions and Programming Constructs
Decision Making and Looping Constructs
Comments
Libraries and Packages
Typedefs
Data structures represented as Collections / Generics
Your First Dart Code
Let us start with the traditional “Hello World” example −
main() {
print("Hello World!");
}
The main() function is a predefined method in Dart. This method acts as the entry point to the application. A Dart script needs the main() method for execution. print() is a predefined function that prints the specified string or value to the standard output i.e. the terminal.
The output of the above code will be −
Hello World!
Execute a Dart Program
You can execute a Dart program in two ways −
Via the terminal
Via the WebStorm IDE
Via the Terminal
To execute a Dart program via the terminal −
Navigate to the path of the current project
Type the following command in the Terminal window
dart file_name.dart
Via the WebStorm IDE
To execute a Dart program via the WebStorm IDE −
Right-click the Dart script file on the IDE. (The file should contain the main() function to enable execution)
Click on the ‘Run <file_name>’ option. A screenshot of the same is given below −
One can alternatively click thebutton or use the shortcut Ctrl+Shift+F10 to execute the Dart Script.
Dart Command-Line Options
Dart command-line options are used to modify Dart Script execution. Common commandline options for Dart include the following −
Sr.No
Command-Line Option & Description
1
-c or –cEnables both assertions and type checks (checked mode).
2
–versionDisplays VM version information.
3
–packages <path>Specifies the path to the package resolution configuration file.
4
-p <path>Specifies where to find imported libraries. This option cannot be used with –packages.
5
-h or –helpDisplays help.
Enabling Checked Mode
Dart programs run in two modes namely −
Checked Mode
Production Mode (Default)
It is recommended to run the Dart VM in checked mode during development and testing, since it adds warnings and errors to aid development and debugging process. The checked mode enforces various checks like type-checking etc. To turn on the checked mode, add the -c or –-checked option before the script-file name while running the script.
However, to ensure performance benefit while running the script, it is recommended to run the script in the production mode.
Consider the following Test.dart script file −
void main() {
int n = "hello";
print(n);
}
Run the script by entering −
dart Test.dart
Though there is a type-mismatch the script executes successfully as the checked mode is turned off. The script will result in the following output −
hello
Now try executing the script with the “- – checked” or the “-c” option −
dart -c Test.dart
Or,
dart - - checked Test.dart
The Dart VM will throw an error stating that there is a type mismatch.
Unhandled exception:
type 'String' is not a subtype of type 'int' of 'n' where
String is from dart:core
int is from dart:core
#0 main (file:///C:/Users/Administrator/Desktop/test.dart:3:9)
#1 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart :261)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)
Identifiers in Dart
Identifiers are names given to elements in a program like variables, functions etc. The rules for identifiers are −
Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
Identifiers cannot include special symbols except for underscore (_) or a dollar sign ($).
Identifiers cannot be keywords.
They must be unique.
Identifiers are case-sensitive.
Identifiers cannot contain spaces.
The following tables lists a few examples of valid and invalid identifiers −
Valid identifiers
Invalid identifiers
firstName
Var
first_name
first name
num1
first-name
$result
1number
Keywords in Dart
Keywords have a special meaning in the context of a language. The following table lists some keywords in Dart.
abstract 1
continue
false
new
this
as 1
default
final
null
throw
assert
deferred 1
finally
operator 1
true
async 2
do
for
part 1
try
async* 2
dynamic 1
get 1
rethrow
typedef 1
await 2
else
if
return
var
break
enum
implements 1
set 1
void
case
export 1
import 1
static 1
while
catch
external 1
in
super
with
class
extends
is
switch
yield 2
const
factory 1
library 1
sync* 2
yield* 2
Whitespace and Line Breaks
Dart ignores spaces, tabs, and newlines that appear in programs. You can use spaces, tabs, and newlines freely in your program and you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.
Dart is Case-sensitive
Dart is case-sensitive. This means that Dart differentiates between uppercase and lowercase characters.
Statements end with a Semicolon
Each line of instruction is called a statement. Each dart statement must end with a semicolon (;). A single line can contain multiple statements. However, these statements must be separated by a semicolon.
Comments in Dart
Comments are a way to improve the readability of a program. Comments can be used to include additional information about a program like author of the code, hints about a function/ construct etc. Comments are ignored by the compiler.
Dart supports the following types of comments −
Single-line comments ( // ) − Any text between a “//” and the end of a line is treated as a comment
Multi-line comments (/* */) − These comments may span multiple lines.
Example
// this is single line comment
/* This is a
Multi-line comment
*/
Object-Oriented Programming in Dart
Dart is an Object-Oriented language. Object Orientation is a software development paradigm that follows real-world modelling. Object Orientation considers a program as a collection of objects that communicate with each other via mechanism called methods.
Object − An object is a real-time representation of any entity. As per Grady Brooch, every object must have three features −
State − described by the attributes of an object.
Behavior − describes how the object will act.
Identity − a unique value that distinguishes an object from a set of similar such objects.
Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object.
Method − Methods facilitate communication between objects.
Example: Dart and Object Orientation
class TestClass {
void disp() {
print("Hello World");
}
}
void main() {
TestClass c = new TestClass();
c.disp();
}
The above example defines a class TestClass. The class has a method disp(). The method prints the string “Hello World” on the terminal. The new keyword creates an object of the class. The object invokes the method disp().