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 Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code. 

Let us understand the syntax for Dart language in detail: 

1. Header files or libraries:

There are various inbuilt functions provided by the Dart language whose definitions are stored in header files or libraries. Whenever we use any in-built function in our program, we need to import the respective header file or library. Header files are written before any function at the beginning of the program.

For example, standard input function stdin.readLineSync( ) has its definition in dart : io header file. 

In order to import a header file, follow this syntax:

import 'dart:io';

2. The main( ) function is the entry point for all dart applications. The execution of a program begins from here.

It is written as follows:

return type main(function arguments)

{

                 Body of the function

     }

Return type refers to the type of value returned by main( ) function. Usually, it is void, int or double. Function arguments are the variables passed to the function. Generally, main( ) doesn't have any arguments. 

For example,

void main( )

{

  print( ‘Hello World!’ );

}

Output:

Hello World!

3. The beginning and end of the program's line of codes are denoted by the curly braces { }. Group of program statements shall always be enclosed within these braces, also known as a block. 

For example,

{

 Statements;

}

4. Every statement of the Dart program is terminated by a terminator or semi-colon ";". It indicates that the statement has ended here.

5. The most integral parts of any language are its variables, methods, classes, and functions. The name that defines these parts is called an identifier. It is a sequence of letters, digits, and some permissible special characters like underscore "_" or dollar "$". There are a few rules to write identifiers in a Dart code which are as follows:

  • Dart is a case-sensitive language, and so are the identifiers.
    For example, Area and area are two different identifiers.
  • Blank spaces are not allowed in the identifiers.
    For example, ‘area circle’ is an invalid identifier while ‘areacircle’ is a valid identifier.
  • Identifiers cannot be the keywords, which are the reserved words of a programming language, and they must be unique.
    For example, case, const, set, etc. are the invalid identifiers as they are the keywords provided by Dart.
  • Identifiers can not include any special character except underscore "_" or dollar "$".
    For example, area_circle, _area, $area, num$, area$, and area$circle are valid keywords while area-circle, num*, and num% are invalid identifiers.
  • The first character can only be an alphabet (lower or upper case) or permissible special characters such as, dollar ‘$’ and underscore ‘_’. But two successive underscores ‘__’ are not allowed.
    For example, _radius, Dividend, $rem are valid keywords while, 5rem, __radius are invalid keywords.

 These are the syntaxes for the declaration of variables and functions : 

1. Variables :

datatype variable_name;

Here, data type refers to the type of value that will be stored in variables like int, double, string, etc. 

For example,

int num;

2. Functions :

return type function_name( arguments )

{

// Body of the function

}

Here, return type refers to the type of value returned by the function. The function name is followed by round parentheses that contain the list of arguments which are variables passed to the function. Note that parameters are optional in the function.

For example,

int area( int r )

{

     int area = 3.14 * r * r;

     return area;

}

Whenever we use user-defined functions in Dart, we need to call them in main( ) to execute them. The statement that calls the function is known as a call statement. If the return type of function is void, then the call statement includes only the function name and variables passed to it as follows:

function_name( variable );

And if the return type is any other type than void, then the call statement is as follows: 

variable = function_name( variable ).

This is so because the value returned by the function has to be received by a variable, calling the function.

In both the cases, variable inside the parentheses refers to the variable we pass as an argument in the function. 

For example:

Consider the following code,

import 'dart:io';

//function to calculate remainder

int compute( int a, int b )

{

  int c = a % b;

  return c;

}

//main function to test

void main( )

{

  int rem;

  print( 'Enter the value of a : ' );

  int a = int.parse( stdin.readLineSync( )! ); // accepts integer value from the user

  print( 'Enter the value of b : ' );

  int b = int.parse( stdin.readLineSync( )! );

  rem = compute( a, b ); // function call statement

  print( 'Value of remainder is $rem' );

}

Output:

Enter the value of a :

10

Enter the value of b :

4

Value of remainder is 2

6. print('Text message goes here');

print( ) function prints the statements or values on the output screen. The text or value to be printed is enclosed within the single or double quotes. 

To print the value of a variable, we use string interpolation '$variable_name'

For example, 

void main( )

{

    float area = 125.5;

    print( 'Value of area is $area' );

}

Output:

Value of area is 125.5