Javatpoint Logo
Javatpoint Logo

Dart Basic Syntax

In this tutorial, we will learn the basic syntax of the Dart programming language.

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.

Below is the table of valid and invalid identifiers.

Valid Identifiers Invalid Identifiers
firstname __firstname
firstName first name
var1 V5ar
$count first-name
_firstname 1result
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. Below is an example.

Example -

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 -

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:

Dart Command-Line Options

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

Sr. Command-line Options Descriptions
1. -c or -c It allows both assertion and type checks.
2. --version It shows VM version information.
3. --package<path> It indicates the path to the package resolution configuration file.
4. -p <path> It indicates where to find the libraries.
5. -h or -help It 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 -

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

The Dart VM will through the following error.


Next TopicDart Comments





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA