Dart Programming Tutorial PDF
Dart Programming Tutorial PDF
Dart Programming Tutorial PDF
Dart is a new programming language meant for the server as well as the browser.
Introduced by Google, the Dart SDK ships with its compiler – the Dart VM. The SDK also
includes a utility -dart2js, a transpiler that generates JavaScript equivalent of a Dart
Script.
This tutorial provides a basic level understanding of the Dart programming language.
Audience
This tutorial will be quite helpful for all those developers who want to develop single-page
web applications using Dart. It is meant for programmers with a strong hold on object-
oriented concepts.
Prerequisites
The tutorial assumes that the readers have adequate exposure to object-oriented
programming concepts. If you have worked on JavaScript, then it will help you further to
grasp the concepts of Dart quickly.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected].
i
Dart Programming
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
ii
Dart Programming
5. DART – VARIABLES............................................................................................................. 15
6. DART – OPERATORS........................................................................................................... 19
Assignment Operators........................................................................................................................... 24
The if Statement.................................................................................................................................... 41
iii
Dart Programming
Parsing .................................................................................................................................................. 48
Number Properties................................................................................................................................ 49
hashcode ............................................................................................................................................... 50
isFinite .................................................................................................................................................. 51
isInfinite ................................................................................................................................................ 51
isNegative ............................................................................................................................................. 52
isEven .................................................................................................................................................... 52
isOdd ..................................................................................................................................................... 53
sign ....................................................................................................................................................... 53
Abs ........................................................................................................................................................ 54
ceil ........................................................................................................................................................ 55
compareTo ............................................................................................................................................ 55
floor ...................................................................................................................................................... 56
remainder ............................................................................................................................................. 57
round .................................................................................................................................................... 57
toDouble ............................................................................................................................................... 58
toInt ...................................................................................................................................................... 59
toString ................................................................................................................................................. 59
truncate ................................................................................................................................................ 60
iv
Dart Programming
codeUnits .............................................................................................................................................. 63
isEmpty ................................................................................................................................................. 64
length .................................................................................................................................................... 64
toLowerCase ......................................................................................................................................... 66
toUpperCase ......................................................................................................................................... 66
trim ....................................................................................................................................................... 67
compareTo ............................................................................................................................................ 68
replaceAll .............................................................................................................................................. 69
split ....................................................................................................................................................... 69
substring ............................................................................................................................................... 70
toString ................................................................................................................................................. 71
codeUnitAt ............................................................................................................................................ 71
v
Dart Programming
List.last .................................................................................................................................................. 80
List.reversed .......................................................................................................................................... 81
List.single .............................................................................................................................................. 81
Map – Properties................................................................................................................................... 91
Keys ...................................................................................................................................................... 92
Values ................................................................................................................................................... 92
length .................................................................................................................................................... 93
isEmpty ................................................................................................................................................. 93
isNotEmpty ........................................................................................................................................... 94
Map ─ Functions.................................................................................................................................... 94
Map.addAll() ......................................................................................................................................... 95
Map.clear() ........................................................................................................................................... 95
Map.remove() ....................................................................................................................................... 96
Map.forEach() ....................................................................................................................................... 97
vi
Dart Programming
vii
Dart Programming
viii
Dart Programming
ix
1. Dart – Overview Dart Programming
Dart is an object-oriented language with C-style syntax which can optionally trans compile
into JavaScript. It supports a varied range of programming aids like interfaces, classes,
collections, generics, and optional typing.
Google has released a special build of Chromium – the Dart VM. Using Dartium means you
don’t have to compile your code to JavaScript until you’re ready to test on other browsers.
This tutorial provides a basic level understanding of the Dart programming language.
10
2. Dart – Environment Dart Programming
This chapter discusses setting up the execution environment for Dart on the Windows
platform.
Dartpad also enables to code in a more restrictive fashion. This can be achieved by checking
the Strong mode option on the bottom right of the editor. Strong mode helps with:
void main() {
print('hello world');
}
11
Dart Programming
hello world
https://www.dartlang.org/install/archive
http://www.gekorm.com/dart-windows/
On completion of the SDK installation, set the PATH environment variable to:
12
Dart Programming
<dart-sdk-path>\bin
Dart
IDE Support
A plethora of IDEs support scripting in Dart. Examples include Eclipse, IntelliJ, and
WebStorm from Jet brains.
Given below are the steps for configuring the Dart environment using WebStrom IDE.
Installing WebStorm
The installation file for WebStorm can be downloaded from
http://www.jetbrains.com/webstorm/download/#section=windows-version.
The WebStorm installation file is available for Mac OS, Windows and Linux.
After downloading the installation files, follow the steps given below:
13
Dart Programming
The dart2js tool is shipped as a part of the Dart SDK and can be found in the /dart-sdk/bin
folder.
This command produces a file that contains the JavaScript equivalent of your Dart code. A
complete tutorial on using this utility can be found on the official Dart website.
14
3. Dart ─ Syntax Dart Programming
Syntax defines a set of rules for writing programs. Every language specification defines its
own syntax. A Dart program is composed of:
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.
Hello World!
15
Dart Programming
dart file_name.dart
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 the button or use the shortcut Ctrl+Shift+F10 to
execute the Dart Script.
16
Dart Programming
Command-Line Description
Option
--packages
Specifies the path to the package resolution configuration file.
<path>
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.
void main()
{
int n="hello";
print(n);
}
17
Dart Programming
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,
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.
18
Dart Programming
The following tables lists a few examples of valid and invalid identifiers −
firstName Var
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.
19
Dart Programming
Dart is Case-sensitive
Dart is case-sensitive. This means that Dart differentiates between uppercase and lowercase
characters.
20
Dart Programming
21