2.dart Programming - Syntax - Tutorialspoint
2.dart Programming - Syntax - Tutorialspoint
Syntax defines a set of rules for writing programs. Every language specification defines its own
syntax. A Dart program is composed of −
Live Demo
main() {
print("Hello World!");
}
The main() function is a predefined method in Dart. This method acts as the entry point to the
application. A Dart script needs the main() method for execution. print() is a predefined function
that prints the specified string or value to the standard output i.e. the terminal.
Hello World!
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 1/7
11/6/2020 Dart Programming - Syntax - Tutorialspoint
dart file_name.dart
Click on the ‘Run <file_name>’ option. A screenshot of the same is given below −
One can alternatively click the button or use the shortcut Ctrl+Shift+F10 to execute the Dart
Script.
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 2/7
11/6/2020 Dart Programming - Syntax - Tutorialspoint
1 -c or --c
Enables both assertions and type checks (checked mode).
2 --version
Displays VM version information.
3 --packages <path>
Specifies the path to the package resolution configuration file.
4 -p <path>
Specifies where to find imported libraries. This option cannot be used with --packages.
5 -h or --help
Displays help.
Checked Mode
Production Mode (Default)
It is recommended to run the Dart VM in checked mode during development and testing, since it
adds warnings and errors to aid development and debugging process. The checked mode enforces
various checks like type-checking etc. To turn on the checked mode, add the -c or –-checked option
before the script-file name while running the script.
However, to ensure performance benefit while running the script, it is recommended to run the
script in the production mode.
Live Demo
void main() {
int n = "hello";
print(n);
}
dart Test.dart
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 3/7
11/6/2020 Dart Programming - Syntax - Tutorialspoint
Though there is a type-mismatch the script executes successfully as the checked mode is turned
off. The script will result in the following output −
hello
Now try executing the script with the "- - checked" or the "-c" option −
dart -c Test.dart
Or,
The Dart VM will throw an error stating that there is a type mismatch.
Unhandled exception:
type 'String' is not a subtype of type 'int' of 'n' where
String is from dart:core
int is from dart:core
#0 main (file:///C:/Users/Administrator/Desktop/test.dart:3:9)
#1 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart
Identifiers in Dart
Identifiers are names given to elements in a program like variables, functions etc. The rules for
identifiers are −
Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
Identifiers cannot include special symbols except for underscore (_) or a dollar sign ($).
Identifiers cannot be keywords.
The following tables lists a few examples of valid and invalid identifiers −
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 4/7
11/6/2020 Dart Programming - Syntax - Tutorialspoint
firstName Var
num1 first-name
$result 1number
Keywords in Dart
Keywords have a special meaning in the context of a language. The following table lists some
keywords in Dart.
Dart ignores spaces, tabs, and newlines that appear in programs. You can use spaces, tabs, and
newlines freely in your program and you are free to format and indent your programs in a neat and
consistent way that makes the code easy to read and understand.
Dart is Case-sensitive
Dart is case-sensitive. This means that Dart differentiates between uppercase and lowercase
characters.
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 5/7
11/6/2020 Dart Programming - Syntax - Tutorialspoint
Each line of instruction is called a statement. Each dart statement must end with a semicolon (;). A
single line can contain multiple statements. However, these statements must be separated by a
semicolon.
Comments in Dart
Comments are a way to improve the readability of a program. Comments can be used to include
additional information about a program like author of the code, hints about a function/ construct etc.
Comments are ignored by the compiler.
Multi-line comments (/* */) − These comments may span multiple lines.
Example
/* This is a
Multi-line comment
*/
Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates
data for the object.
Method − Methods facilitate communication between objects.
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 6/7
11/6/2020 Dart Programming - Syntax - Tutorialspoint
Live Demo
class TestClass {
void disp() {
print("Hello World");
}
}
void main() {
TestClass c = new TestClass();
c.disp();
}
The above example defines a class TestClass. The class has a method disp(). The method prints
the string “Hello World” on the terminal. The new keyword creates an object of the class. The object
invokes the method disp().
Hello World
https://www.tutorialspoint.com/dart_programming/dart_programming_syntax.htm 7/7