Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are:

1. Using IDE = Visual Studio code is a text editor with IDE. All you have to do is download the Dart extension, write your code with the .Dart extension and compile using the same.

2. Online Compiler = DartPad is an online compiler provided by the Dart language to code and compile the program in Dart.

3. Using Command line = After downloading Dart SDK, one can easily code in Dart using Dart specific commands in the command line client.

In case you haven’t downloaded it, visit the previous tutorial that discusses the installation of the above platforms in detail.

Let’s write our first program in Dart.

Program:

//This is the first Dart program

void main( )

{

  var a = 5, b = 6 ;

  var c = a + b ;

  print( ' Sum of two numbers is $c. ' ) ;

}
  • Using Visual Studio Code
    • Open Visual Studio Code.
    • Type your code.
    • Save the file with the .Dart extension.
    • Run the code using F5 or the run button on the top right corner.
Dart Basic Program
  • Using online compiler DartPad.
    • Search on Google DartPad.
    • Type your code and run it.
Dart Basic Program
  • Using the command line.
    • Open notepad and type your code.
    • Save the file with the file name as ‘name.Dart’. .Dart to recognize that this is a Dart file.
    • Open the command prompt and write the command ‘Dart filename’. Here, the ‘filename’ is to be replaced with the name of your file.
    • Press enter, and output will be displayed.
Dart Basic Program Dart Basic Program

Let’s understand the program in detail.

This program initializes two numbers, adds them and print their sum.

//This is the first Dart program

void main( )

{

  var a = 5, b = 6;

  var c = a + b;

  print( 'Sum of two numbers is $c.' );

}
  1. This is the first Dart program

The first line beginning with ‘//’ is a single-line comment. Comments do not get executed but are written in order to make the code more descriptive and easier to understand.  

Dart also supports multi-line comments in case of multiple lines as follows:

/*…………………

……………………*/

  • main( )

Like in C, the execution of the program always and only begins from the main( ) function.

  • void

Return type of the function main( ) is void. Return type informs the compiler about the type of value the function will return. void means that the function does not return any value.
It can have other return types as well like, int, double, etc.

  • The whole program is contained within the curly braces { }.  Remember that group of programming statements will always be enclosed within the curly braces {  }.
  • var a = 5, b = 6;

This is the single line, multiple variable initialization statement. ‘var’ is a keyword used to declare a variable without specifying any data type. The Dart compiler automatically interprets the data type of the variable by looking at the value initialized to it. For example, here, the values are integer type.

We can specify the data type explicitly also instead of var keyword. Initialization will look like this in that case,

int a = 5, b = 6;

  • var c = a + b;

This is an assignment statement that performs the arithmetic operation, addition on variables ‘a’ and ‘b’ using plus (addition)  operator ‘+’, and assigns the value of sum on the right-hand side to the variable ‘c’ on the left-hand side using assignment operator ‘=’.

An important point to remember is that in the case of an assignment statement, the value on the right-hand side is always assigned to the variable on the left-hand side. The converse is not true.

  • print(‘Sum of two numbers is $c’);

print is an inbuilt function to display output on the output screen. The text to be printed on the output screen is enclosed within single quotes ‘   ’. ‘$’ operator is used to convert an expression to a string using string interpolation.

  • Remember that every statement is terminated using a terminator ‘;’.