Every programming language has its own set of basic rules, and Dart is no exception.
\\n\\nThis chapter introduces the basic structure of Dart programs, comment syntax, the difference between statements and expressions, and naming conventions for identifiers.
\\n\\n\\n\\n
Program Structure and the main() Function
\\n\\nDart files have a .dart extension, and every Dart program must have an entry pointβthe main() function.
The program starts executing from the first line of the main() function and ends at the closing brace of the last line.
Example
\\n\\nThe structure of a minimal Dart program:
\\n\\n// Program entry: the main() function is the starting point of a Dart program\\n\\nvoid main(){\\n \\n // Write your code here\\n \\n print('Welcome to TUTORIAL Dart Tutorial!');\\n}\\n\\n\\nOutput:
\\n\\nWelcome to TUTORIAL Dart Tutorial!\\n\\n\\nLet's break down each part of this code:
\\n\\n| Code Element | \\nDescription | \\n
|---|---|
void | \\nReturn type; void means the function does not return any value | \\n
main | \\nFunction name; this is the required entry point name in Dart and cannot be changed | \\n
() | \\nParameter list; main() can accept no parameters or command-line arguments | \\n
{ } | \\nFunction body; the code inside the braces is what the function executes | \\n
print() | \\nBuilt-in function for outputting text to the console | \\n
; | \\nStatement terminator; every statement in Dart must end with a semicolon | \\n
\\n\\n\\nDart strictly requires every statement to end with a semicolon (
\\n;). Forgetting semicolons is the most common syntax error for beginners. Fortunately, the Dart plugin for VS Code will automatically prompt you where semicolons are missing.
main() Function with Command-Line Arguments
\\n\\nThe main() function can accept a list of strings as command-line arguments:
Example
\\n\\n// List<String> args Receives the list of parameters passed from the command line\\n\\nvoid main(List<String> args){\\n \\n // Output the number of parameters\\n print('ParameterCount:${args.length}');\\n \\n // Iterate and print each parameter\\n for(var i = 0; i < args.length; i++){\\n print('Parameter $i: ${args}');\\n }\\n}\\n\\n\\nExecution and output:
\\n\\n$ dart run hello.dart tutorial dart Tutorial\\nParameterCount:3\\nParameter 0: tutorial\\nParameter 1: dart\\nParameter 2: Tutorial\\n\\n\\nThis uses the List type and for loop, which we will cover in detail in later chapters.
\\n\\n
Comment Syntax
\\n\\nComments are explanatory notes for code, and the compiler ignores them.
\\n\\nGood comments help others (including yourself months later) understand the intent of the code more quickly.
\\n\\nDart supports three types of comments:
\\n\\n| Type | \\nSyntax | \\nUse Case | \\n
|---|---|---|
| Single-line comment | \\n// comment content | \\nBrief explanation of a line of code | \\n
| Multi-line comment | \\n/* comment content */ | \\nDetailed explanations spanning multiple lines | \\n
| Documentation comment | \\n/// documentation comment | \\nGenerate API documentation for functions and classes | \\n
Example
\\n\\n/// This is a documentation comment, used to describe the program's functionality\\n/// Documentation comments can use Command to generate HTML documentation\\n\\nvoid main(){\\n // Single-line comment: output welcome message\\n print('Welcome to learning Dart!'); // Inline comment: this is also valid\\n \\n /*\\n * Multi-line comment: suitable for longer explanations\\n * Second line comment\\n * Third line comment\\n */\\n \\n print('TUTORIAL Tutorial - Dart Basic Syntax');\\n}\\n\\n\\nOutput:
\\n\\nWelcome to learning Dart!\\nTUTORIAL Tutorial - Dart Basic Syntax\\n\\n\\n\\n\\n\\nThe principle of comments is "explain why, not what." The code itself already explains what it does; comments should explain why it was written that way, or provide background information that the code cannot express.
\\n
\\n\\n
Statements and Expressions
\\n\\nIn Dart, code consists of Statements and Expressions.
\\n\\nUnderstanding the difference between the two helps you better organize your code logic.
\\n\\nExpressions
\\n\\nAn expression is a piece of code that can be evaluated to produce a value.
\\n\\nExample
\\n\\nvoid main(){\\n // Each line below is an expression, and they all produce a value\\n \\n 42; // Literal expression, value is 42\\n 3 + 5; // Arithmetic expression, value is 8\\n 'Hello'.length; // Property access expression, value is 5\\n (10 > 5); // Relational expression, value is true\\n}\\n\\n\\nStatements
\\n\\nA statement is an instruction that performs an action; it does not produce a value.
\\n\\nExample
\\n\\nvoid main(){\\n // Variable declaration statement: declare a variable and assign a value\\n var name = 'TUTORIAL';\\n \\n // Function call statement: call print() to perform output action\\n print(name);\\n \\n // Control flow statement: decides which code to execute based on conditions\\n if(name.length > 3){\\n print('Name length greater than 3');\\n }\\n}\\n\\n\\nOutput:
\\n\\nTUTORIAL\\nName length greater than 3\\n\\n\\nIn short: expressions produce values, statements perform actions.
\\n\\nYou can use expressions where a value is needed, but you cannot use statements.
\\n\\n\\n\\n\\nA common misconception is treating all code as "statements." In fact, many constructs in Dart (such as function calls and variable assignments) are themselves expressions, meaning they can be nested. This is a characteristic shared by Dart and other C-style languages.
\\n
\\n\\n
Identifier Naming Conventions
\\n\\nAn identifier is the name you give to variables, functions, classes, and other code elements.
\\n\\nDart has rules and conventions for identifiers:
\\n\\nNaming Rules (Must Follow)
\\n\\n- \\n
- Identifiers can only contain letters, digits, underscores (
_), and dollar signs ($). \\n - Identifiers cannot start with a digit. \\n
- Identifiers cannot be Dart reserved keywords (such as
class,if,var, etc.). \\n
Naming Conventions (Recommended)
\\n\\n| Code Element | \\nNaming Style | \\nExamples | \\n
|---|---|---|
| Class names | \\nUpperCamelCase | \\nStudentInfo, HttpClient | \\n
| Enum types | \\nUpperCamelCase | \\nColor, Status | \\n
| Library names, file names | \\nlowercase_with_underscores | \\nmy_utils, user_service | \\n
| Variable names | \\nlowerCamelCase | \\nuserName, maxCount | \\n
| Function names | \\nlowerCamelCase | \\ngetUserInfo(), calculateTotal() | \\n
| Constant names | \\nlowerCamelCase | \\npiValue, defaultPort | \\n
| Private members | \\nLeading underscore | \\n_internalState, _privateMethod() | \\n
Example
\\n\\nPractical application of various naming styles:
\\n\\n// Class name: UpperCamelCase\\nclass UserAccount {\\n \\n // Public members: lowerCamelCase\\n String userName;\\n \\n // Private members: underscore prefix (only accessible within this file)\\n String _password;\\n \\n // Constructor\\n UserAccount(this.userName, this._password);\\n \\n // Public methods: lowerCamelCase\\n bool verifyPassword(String input){\\n return input == _password;\\n }\\n \\n // Private method: underscore prefix\\n void _encryptPassword(){\\n // Encryption logic...\\n }\\n}\\n\\n// Entry function: lowercase\\nvoid main(){\\n \\n // Variable name: lower camel case\\n var userAccount = UserAccount('tutorial', '123456');\\n \\n // Constants: lowerCamelCase (Dart style)\\n const maxLoginAttempts = 3;\\n \\n print('Username: ${userAccount.userName}');\\n print('Maximum login attempts: $maxLoginAttempts');\\n}\\n\\n\\nOutput:
\\n\\nUsername: tutorial\\nMaximum login attempts: 3\\n\\n\\n\\n\\n\\nIn Dart's naming conventions, constants use lowerCamelCase, not the all-uppercase with underscores (UPPER_CASE) common in other languages. This is a unique style of the Dart community, so please take note.
\\n
YouTip