Dart Syntax

Syntax defines a set of rules for writing programs. Every language specification defines its own syntax.

Dart 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.

  • The first character should not be a digit.

  • Special characters are not allowed except underscore (_) or a dollar sign ($).

  • Two successive underscores (__) are not allowed.

  • The first character must be alphabet(uppercase or lowercase) or underscore.

  • Identifiers must be unique and cannot contain whitespace.

  • They are case sensitive. The variable name Joseph and joseph will be treated differently.

Valid IdentifiersInvalid Identifiers
firstname__firstname
firstNamefirst name
var1V5ar
$countfirst-name
_firstname1result
First_name@var

Dart Printing and String Interpolation

The print() function is used to print output on the console, and $expression is used for the string interpolation.

Example

void main()  
{  
    var name = "Peter";  
    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

Semicolon in Dart

  • The semicolon is used to terminate the statement that means, it indicates the statement is ended here.

  • It is mandatory that each statement should be terminated with a semicolon(;).

  • We can write multiple statements in a single line by using a semicolon as a delimiter.

  • The compiler will generate an error if it is not use properly.

Example

var msg1 = "Hello World!";  
var msg2 = "How are you?"  

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

{ //start of the block   
  
    //block of statement(s)  
  
}// end of the block  

Dart Command-Line

The Dart command-line options are used to modify Dart script execution. The standard command-line options are given below.

Sr.Command-line OptionDescriptions
1.-c or -cIt allows both assertion and type checks.
2.–versionIt shows VM version information.
3.–packageIt indicates the path to the package resolution configuration file.
4.-p It indicates where to find the libraries.
5.-h or -helpIt is used to seek help.

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.

Example

void main() {   
   int var = "hello";   
   print(var);   
}  

Now activate the checked mode by typing dart -c or –checked mode

dart -c mode.dart  

The Dart VM will through the following error.

Unhandled exception:   
type 'String' is not a subtype of type 'int' of 'n' where   
String is from dart:core   
int is from dart:core