Open In App

Dart – Basic Syntax

Improve
Improve
Like Article
Like
Save
Share
Report

Dart is a static programming language developed by Google. According to the GitHub popularity index, it became the most popular programming language as it actually supports the flutter toolkit. Flutter is a framework that uses dart’s native compilation ability to generate fast cross-platform apps. Dart supports two types of compilation that are, Just in time and Ahead of time. Its syntax has basically a blend of CPP, Python, Java, and JavaScript. In this blog, We will look at the basic syntax in Dart and how to represent the language that the computer understands. We will see the basic structure of Dart programming. 

The syntax is a basic dart program that consists of various elements such as a keyword, an identifier, a constant, a string literal, data types, and symbols. Eg: to represent numbers words and even decimals we can call the type of data. Each line is dart must end with the semicolon.

Dart




void main() {
   
  // declare and initialized a variable
  int A = 2;
  int B = 3;
   
  // displaying the output
  print( A + B );
}


 
 

To represent this we can tell the type of the data, in the above example as an integer type. Similarly, To represent a word or string we can call the data type a string. All these are called Variables. Now the integers, strings, and even decimal values are temporarily held by a word or character like ‘A’ and ‘B’. All these are called variables because the data inside can keep changing. The word to the left of the ‘=’ is the variable while the one in the right is the actual value.

 

Comments in Dart

A comment is a code snippet that is not readable by the compiler. Comments help to write a code efficiently and make it easier to understand the code. Comments in dart can be obtained using one of the following:

 

' // ' is used to comment on a single line.

'/*.....*/ is used to comment on multiple lines.

 

Dart




void main() {
   
  // Displaying the output
  print('Hello, Geeks!');
   
  /* print('Dart is a programming language');
  print('Dart is a programming language'); */
  print('Hello, Geeks!');
}


 
 

Keywords in Dart:

 

Keywords are the words that are used a dart to represent the basics syntax that the compiler will understand. The basic keywords that are used in dart programs are listed below:

 

import else as abstract switch super in
enum assert export const var void static
do async await catch on null new
class break continue false new typedef true
get hide set show dynamic return rethrow

Apart from this, there are other keywords that can be used as the syntax, but they are rarely used.

 

Identifiers in Dart:

Identifiers are the name given to variables that can be read by the compiler. There are certain rules to write the identifiers:

 

  • No special symbol can be used except _ and $.
  • It can not be the reserved keywords.
  • There should not be any space between the variable name.
  • They are case sensitives.

Variables and Data types in Dart:

Variables are the name given to a memory location declaring by writing variable-type _ name and then initialized and declared by ” variable-type name = value; “.

 

The dart language supports the following built-in types :

 

  • String

 

A Dart string is a collection of characters and letters. You can use either single or double quotes to create a string. For multi-line string, we can use ‘….’ or “…..”. We can give the value of a character inside a string by doing $ {expression}. 

 

Dart




var name = 'John';


 
 

Here a variable is called a name and the string is assigned a value “John”.

 

Note: If a variable is not initialized then the default value is null 
  • Numbers 

 

Both int and double are can be used for the representation of numbers. These data types are used to represent any number and even a decimal. The num type operates with basic algebraic operators.

 

Dart




void main() {
   
  // declare and initialized a variable
  int A = 20;
  double B = 30.0;
   
  // displaying the output
  print( A + B );
}


 
 

  • Booleans

 

In order to use the Boolean data types, dart uses the keyword named bool. These data types have two values that are True and False.

 

Dart




void main() {
 bool isIttrue1 = true;
 print('$isIttrue1');
}


  • Collection [List, Set, Maps]

Collections in Dart is basically a collection of similar and different data types. These are used as data structures in dart. Each element can be accessed sequentially.

Dart




void main() {
  var list = [10, 20, 30];
  var names = ['Jack','Jill', 10, 1];
}


Operators in Dart:

Operators representation is really simple. The basic addition of numbers is possible by inserting the + operator. Similarly, we can insert any operators including BIT operation simply calling the operator on two values and assign it to another value. Let see some basics operators used in dart programming.

  • Algebraic Operators

These operators can be used between two values and assign to the third one. The operators are listed below:

  1. Addition -> “+” Operator
  2. Subtraction -> “-” Operator
  3. Multiplication -> “*” Operator
  4. Division -> “/” Operator
  5. Modular -> “%” Operator
  • Relational Operators

The below-listed operators are used to establish a relationship between two objects:

  1. Equal -> “==” Operator
  2. Not Equal -> “!=” Operator
  3. Greater than -> “>” Operator
  4. Less than -> “<“ Operator
  5. Greater than or equal to -> “>=” Operator
  6. Less than or Equal to -> “<=” Operator
  • Logical Operators 

These operators are used to establish logic among objects or values. These operators are listed below:

  1. Logical AND -> “&&” Operator
  2. Logical OR -> “||” Operator
  3. Not -> “!” operator
  • Assignment Operators

The operators are used to assign values. These are listed below:

  1. Assignment -> “=” Operator
  2. Increment -> “+=” Operator
  3. Decrement -> “-=” Operator
  4. Product -> “*=” Operator
  5. Division -> “/=” Operator

=, +=, -=, *=, /= , etc  called as assignment operators.

  • Null Aware Operators

These operators are used to add Null aware objects. They are listed below:

  1.  ?
  2. ??
  3. ??=


Last Updated : 27 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads