Dart Basic Syntax
We will study the fundamentals of the Dart programming language syntax in this course.
Dart Identifiers
The names used to define variables, methods, classes, functions, etc. are known as identifiers. A series of letters ([A to Z], [a to z]), numerals ([0–9]), and underscores (_) make up an identifier; however, the first character shouldn’t be a number. The following list contains the rules for defining identifiers.
- No digit should appear as the initial character.
- The only permitted special characters are the underscore (_) and the dollar sign ($).
- It is not permitted to use two underscores (__) in a row.
- The initial character needs to be underscore or the alphabet in either uppercase or lowercase.
- Whitespace is not permitted in identifiers and they must be unique.
- Case matters for them. Joseph and joseph, the variable names, will be handled differently.
The table with valid and incorrect identifiers is below.
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 output is printed on the console using the print() function, and the string interpolation is done using $expression. Here’s an illustration.
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 signify that the statement has concluded, ending the sentence. A semicolon(;) must always come after a statement. When we use a semicolon as a delimiter, we can write several statements on a single line. If the compiler is not used correctly, an error will be produced.
Example
var msg1 = "Hello World!";
var msg2 = "How are you?"
Dart Whitespace and Line Breaks
Whitespaces are ignored by the Dart compiler. In our software, it is used to specify tabs, spaces, and newline characters. It divides a statement’s constituent parts into distinct sections. To specify indentation and provide the program the right format, we can also utilize tabs and space in our code. It facilitates the reading and understanding of code.
Block in Dart
The collection of statements encased in curly braces is called a block. Curly braces are used in Dart to organize all of the statements that make up a block. Think about the syntax that follows.
Syntax:
{ //start of the block
//block of statement(s)
}// end of the block
Dart Command-Line Options
To alter the way a Dart script is executed, utilize the Dart command-line parameters. The following list contains the common command-line options.
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
The two modes in which the Dart program typically operates are listed below.
- Checked Mode
- Production Mode
Checked Mode: This mode allows the Dart code to undergo several checks, including type checking. When designing processes, it throws faults or issues warnings. Type -c or the –checked option in the command prompt before the name of the dart script file to launch the checked mode. The Dart VM operates in the checked mode by default.
Production Mode: The production mode is used by the Dart script. It guarantees improved performance while the script is executing. Think about the example that follows.
Example
void main() {
int var = "hello";
print(var);
}
Enter dart -c or –checked mode to now enter the checked mode.
dart -c mode.dart
Following is the error that the Dart VM will encounter.
Unhandled exception:
type 'String' is not a subtype of type 'int' of 'n' where
String is from dart:core
int is from dart:core