Dart01 Introduction
Dart01 Introduction
SE SSION-1
WWW.SINET.IN
What is Dart?
Dart is a general-purpose, high-level modern programming language which is originally developed by Google. It is the new
programming language which is emerged in 2011, but its stable version was released in June 2017. Dart is not so popular at th at
time, but It gains popularity when it is used by the Flutter.
Dart is a dynamic, class-based, object-oriented programming language with closure and lexical scope. Syntactically, it is quite
similar to Java, C, and JavaScript. If you know any of these programming languages, you can easily learn the Dart programming
language.
Dart is an open-source programming language which is widely used to develop the mobile application, modern web -applications,
desktop application, and the Internet of Things (IoT) using by Flutter framework. It also supports a few advance concepts such as
interfaces, mixins, abstract classes, refiled generics, and type interface. It is a compiled language and supports two types of
compilation techniques.
• AOT (Ahead of Time) - It converts the Dart code in the optimized JavaScript code with the help of the dar2js compiler and
runs on all modern web-browser. It compiles the code at build time.
• JOT (Just-In-Time) - It converts the byte code in the machine code (native code), but only code that is necessary.
WWW.SINET.IN
History
Dart was revealed for the first time in the GOTO conference in the month of 10th - 12th October 2011 at Aarhus,
Denmark. It is initially designed by the Lars bark and Kespar and developed by Google.
The first version 1.0 of Dart was released on November 14th, 2013, intended as a replacement of JavaScript.
In July 2014, the first edition of Dart language was approved by Ecma International approved at its 107th General
Assembly.
The first version was criticized due to a malfunction on the web and this plan was dropped in 2015 with the 1.9
release of Dart.
The second version of Dart 2.0 was released in August, including a sound type system.
The recent version Dart 2.7 is supplemented with the extension method, which enables us to add any type of
functionality.
WWW.SINET.IN
Why Dart?
We define the characteristics of Dart in the following point.
• Dart is a platform-independent language and supports all operating systems such as Windows, Mac, Linux, etc.
• It is an open-source language, which means it available free for everyone. It comes with a BSD license and
recognized by the ECMA standard.
• It is an object-oriented programming language and supports all features of oops such as inheritance,
interfaces, and optional type features.
• Dart is very useful in building real-time applications because of its stability.
• Dart comes with the dar2js compiler which transmits the Dart code into JavaScript code that runs on all
modern web browser.
• The stand-alone Dart VM permits Dart code to run in a command-line interface environment.
WWW.SINET.IN
Key Points to Remember
Before learning the Dart, we should keep these concepts in mind. These concepts are given below.
• Everything in Dart is treated as an object including, numbers, Boolean, function, etc. like Python. All
objects inherit from the Object class.
• Dart tools can report two types of problems while coding, warnings and errors. Warnings are the
indication that your code may have some problem, but it doesn't interrupt the code's execution,
whereas error can prevent the execution of code.
• Dart supports generic types, like List<int>(a list of integers) or List<dynamic> (a list of objects of any
type).
WWW.SINET.IN
Features
The Dart is an object-oriented, open-source programming language which contains many useful features. It
is the new programming language and supports an extensive range of programming utilities such as
interface, collections, classes, dynamic and optional typing. It is developed for the server as well as the
browser. Below is the list of the important Dart features.
Open Source
Platform Independent
Dart supports all primary operating systems such as Windows, Linux, Macintosh, etc. The Dart has its own
Virtual Machine which known as Dart VM, that allows us to run the Dart code in every operating system.
WWW.SINET.IN
Object-Oriented
Dart is an object-oriented programming language and supports all oops concepts such as classes, inheritance,
interfaces and optional typing features. It also supports advance concepts like mixin, abstract, classes, reified
generic, and robust type system.
Concurrency
Dart is an asynchronous programming language, which means it supports multithreading using Isolates. The isolates
are the independent entities that are related to threads but don't share memory and establish the communication
between the processes by the message passing. The message should be serialized to make effective
communication. The serialization of the message is done by using a snapshot that is generated by the given object
and then transmits to another isolate for desterilizing.
Extensive Libraries
Dart consists of many useful inbuilt libraries including SDK (Software Development Kit), core, math, async, math,
convert, html, IO, etc. It also provides the facility to organize the Dart code into libraries with proper namespacing. It
can reuse by the import statement.
Easy to learn
As we discussed in the previous section, learning the Dart is not the Hercules task as we know that Dart's syntax is
similar to Java, C#, JavaScript, kotlin, etc. if you know any of these languages then you can learn easily the Dart
WWW.SINET.IN
Flexible Compilation
Dart provides the flexibility to compile the code and fast as well. It supports two types of compilation processes,
AOT (Ahead of Time) and JIT (Just-in-Time). The Dart code is transmitted in the other language that can run in the
modern web-brewers.
Type Safe
The Dart is the type safe language, which means it uses both static type checking and runtime checks to confirm
that a variable's value always matches the variable's static type, sometimes it known as the sound typing.
Although types are required, type annotations are optional because of type interference. This makes it code more
readable. The other advantage to being type-safe language is, when we change the part of code, the system
warns us about that modification that we have modified earlier.
Objects
The Dart treats everything as an object. The value which assigns to the variable is an object. The functions,
numbers, and strings are also an object in Dart. All objects inherit from Object class.
Browser Support
The Dart supports all modern web-browser. It comes with the dart2js compiler that converts the Dart code into
optimized JavaScript code that is suitable for all type of web-browser.
Community
Dart has a large community across the world. So if you face problem while coding then it is easy to find help. The
dedicated developers' team is working towards enhancing its functionality.
WWW.SINET.IN
Install the Dart SDK on Windows
Follow the below instructions to install Dark SDK in Windows.
Step -1: Go to the browser and type the following link to download the SDK.
http://www.gekorm.com/dart-windows/
Step - 2: Run the Dart installer(It is the .exe file that we downloaded in the previous step) and click on
the Next button.
Step - 3: It provides the option to select the Dart installation path. After the path is selected, click on
the Next button.
Step - 4: After the download is completed, set the PATH environment variable to "C:\Program Files\Dart\dart-
sdk\bin" in advance system properties.
Step - 5: Now open the terminal and verify the Dart installation by typing dart.
WWW.SINET.IN
First Program
Using Command Line
Step - 1:Type dart on the terminal if it is showing dart runtime then Dart is successfully installed.
Step - 2:Open a text editor and create a file called "helloword.dart". The file extension should be .dart that specifies; it is a Dart program file.
main()
Print(“hello world”);
• main() - The main() function indicates that it is the beginning of our program. It is an essential function where the program st arts its execution.
• print() - This function is used to display the output on the console. It is similar to C, JavaScript, or any other language. The curly brackets and
semicolons are necessary to use appropriately.
Step - 3:
Open the command line; compile the program run the Dart program by typing dart helloworld.dart. It will show Hello World on the screen.
WWW.SINET.IN
Running on Browser
Dart provides an online editor which is known as DartPad available at https://dartpad.dartlang.org/
On the left side, we can write the code and the output displays on the right side of the screen. We can
add HTML and CSS with the Dart Code. Dart also provides some sample programs to learn
WWW.SINET.IN
Using IDE
There are various IDEs that support Dart, such as Visual Studio Code, WebStorm, IntelliJ, etc. For the visual
studio code, download the dart extension and run the code.
main()
{
print("Hello world");
print("Dart Programming");
}
WWW.SINET.IN
Tokens
The smallest individual unit in a program is known as a token. There are five types of tokens
• Keywords
• Identifiers
• Literals
• Punctuators ,:;()[]….
• Operators
WWW.SINET.IN
Keywords
Dart Keywords are the reserve words that have special meaning for the compiler. It cannot be used as the
variable name, class name, or function name. Keywords are case sensitive; they must be written as they are
defined. There are 61 keywords in the Dart. Some of them are common, you may be already familiar and
few are different. Below is the list of the given Dart keywords.
WWW.SINET.IN
abstract else import super
as enum in switch
assert export interface sync
async extends is this
await extension library throw
break external mixin true
case factory new try
catch false null typedef
class final on1 var
const finally operator void
continue for part while
covarient Function rethrow with
default get return yield
deffered2 hide set
do if show
dynamic implements static
WWW.SINET.IN
Identifiers
Identifiers are the name which is used to define variables, methods, class, and function, etc. An Identifier is a
sequence of the letters([A to Z],[a to z]), digits([0-9]) and underscore(_), but remember that the first character
should not be a numeric. There are a few rules to define identifiers which are given below.
WWW.SINET.IN
Valid Identifiers Invalid Identifiers
firstname __firstname
firstName first name
var1 V5ar
$count first-name
_firstname 1result
First_name @var
WWW.SINET.IN
LITERALS (CONSTANTS)
Literals are data items that are fixed data values. There are six types of literals:
• Integer Literals
· Decimal
· Octal
· Hexadecimal
• Floating Literals
• Boolean Literals
• Character Literals
• String Literals
WWW.SINET.IN
Dart Basic Syntax
We will learn the basic syntax of the Dart programming language.
Dart Printing and String Interpolation
The print() function is used to print output on the console, and $expression is used for the string
interpolation. Below is an example.
void main()
{
var name = "Ram";
var roll_no = 24;
print("My name is ${name} My roll number is ${roll_no}");
}
Output:
My name is Peter My roll number is 24
WWW.SINET.IN
Dart Whitespace and Line Breaks
The Dart compiler ignores whitespaces. It is used to specify space, tabs, and newline characters in our
program. It separates one part of any statement from another part of the statement. We can also use space
and tabs in our program to define indentation and provide the proper format for the program. It makes
code easy to understand and readable.
Block in Dart
The block is the collection of the statement enclosed in the curly braces. In Dart, we use curly braces to
group all of the statements in the block. Consider the following syntax.
Syntax:
//block of statement(s)
WWW.SINET.IN
Enable Checked Mode
Generally, the Dart program runs in two modes which are given below.
• Checked Mode
• Production Mode
Checked Mode - The checked mode enables various checks in the Dart code, such as type-checking. It warns or
throws errors while developing processes. To start the checked mode, type -c or --checked option in the
command prompt before the dart script-file name. By default, the Dart VM runs in the checked mode.
Production Mode - The Dart script runs in the production mode. It provides assurance of performance
enhancement while running the script. Consider the following example.
void main() {
print(var);
WWW.SINET.IN
Now activate the checked mode by typing dart -c or --checked mode
dart -c mode.dart
Unhandled exception:
WWW.SINET.IN
Types of Comments
Dart provides three kinds of comments
• Single-line Comments
• Multi-line Comments
• Documentation Comments
Single-line Comment
We can apply comments on a single line by using the // (double-slash). The single-line comments can be
applied until a line break.
Multi-line Comment
Sometimes we need to apply comments on multiple lines; then, it can be done by using /*…..*/. The
compiler ignores anything that written inside the /*…*/, but it cannot be nested with the multi-line
comments. Let's see the following example.
/* This is the example of multi-line comment
This will print the given statement on screen */
WWW.SINET.IN
Dart Documentation Comment
The document comments are used to generate documentation or reference for a project/software
package. It can be a single-line or multi-line comment that starts with /// or /*. We can use /// on
consecutive lines, which is the same as the multiline comment. These lines ignore by the Dart
compiler expect those which are written inside the curly brackets. We can define classes,
functions, parameters, and variables.
///This
///is
///a example of
/// multiline comment
WWW.SINET.IN
END
WWW.SINET.IN