Dart – Basic Syntax

27 Jun, 2021
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.
 
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 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:
 
Apart from this, there are other keywords that can be used as the syntax, but they are rarely used.
 
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 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 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:
Addition -> “+” OperatorSubtraction -> “-” OperatorMultiplication -> “*” OperatorDivision -> “/” OperatorModular -> “%” Operator
Addition -> “+” Operator
Subtraction -> “-” Operator
Multiplication -> “*” Operator
Division -> “/” Operator
Modular -> “%” Operator
Relational Operators
The below-listed operators are used to establish a relationship between two objects:
Equal -> “==” OperatorNot Equal -> “!=” OperatorGreater than -> “>” OperatorLess than -> “<“ OperatorGreater than or equal to -> “>=” OperatorLess than or Equal to -> “<=” Operator
Equal -> “==” Operator
Not Equal -> “!=” Operator
Greater than -> “>” Operator
Less than -> “<“ Operator
Greater than or equal to -> “>=” Operator
Less than or Equal to -> “<=” Operator
Logical Operators 
These operators are used to establish logic among objects or values. These operators are listed below:
Logical AND -> “&&” OperatorLogical OR -> “||” OperatorNot -> “!” operator
Logical AND -> “&&” Operator
Logical OR -> “||” Operator
Not -> “!” operator
Assignment Operators
The operators are used to assign values. These are listed below:
Assignment -> “=” OperatorIncrement -> “+=” OperatorDecrement -> “-=” OperatorProduct -> “*=” OperatorDivision -> “/=” Operator
Assignment -> “=” Operator
Increment -> “+=” Operator
Decrement -> “-=” Operator
Product -> “*=” Operator
Division -> “/=” Operator
=, +=, -=, *=, /= , etc  called as assignment operators.
Null Aware Operators
These operators are used to add Null aware objects. They are listed below:
 ?????=
 ?
??
??=
simranarora5sos
Dart-basics
Picked
Technical Scripter 2020
Dart
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.