YouTip LogoYouTip

Dart Basic Syntax

Every programming language has its own set of basic rules, and Dart is no exception.

\\n\\n

This 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\\n

Dart files have a .dart extension, and every Dart program must have an entry pointβ€”the main() function.

\\n\\n

The program starts executing from the first line of the main() function and ends at the closing brace of the last line.

\\n\\n

Example

\\n\\n

The 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\\n

Output:

\\n\\n
Welcome to TUTORIAL Dart Tutorial!\\n
\\n\\n

Let's break down each part of this code:

\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
Code ElementDescription
voidReturn type; void means the function does not return any value
mainFunction name; this is the required entry point name in Dart and cannot be changed
()Parameter list; main() can accept no parameters or command-line arguments
{ }Function body; the code inside the braces is what the function executes
print()Built-in function for outputting text to the console
;Statement terminator; every statement in Dart must end with a semicolon
\\n\\n
\\n

Dart strictly requires every statement to end with a semicolon (;). 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.

\\n
\\n\\n

main() Function with Command-Line Arguments

\\n\\n

The main() function can accept a list of strings as command-line arguments:

\\n\\n

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\\n

Execution and output:

\\n\\n
$ dart run hello.dart tutorial dart Tutorial\\nParameterCount:3\\nParameter 0: tutorial\\nParameter 1: dart\\nParameter 2: Tutorial\\n
\\n\\n

This uses the List type and for loop, which we will cover in detail in later chapters.

\\n\\n
\\n\\n

Comment Syntax

\\n\\n

Comments are explanatory notes for code, and the compiler ignores them.

\\n\\n

Good comments help others (including yourself months later) understand the intent of the code more quickly.

\\n\\n

Dart supports three types of comments:

\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
TypeSyntaxUse Case
Single-line comment// comment contentBrief explanation of a line of code
Multi-line comment/* comment content */Detailed explanations spanning multiple lines
Documentation comment/// documentation commentGenerate API documentation for functions and classes
\\n\\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\\n

Output:

\\n\\n
Welcome to learning Dart!\\nTUTORIAL Tutorial - Dart Basic Syntax\\n
\\n\\n
\\n

The 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
\\n\\n

Statements and Expressions

\\n\\n

In Dart, code consists of Statements and Expressions.

\\n\\n

Understanding the difference between the two helps you better organize your code logic.

\\n\\n

Expressions

\\n\\n

An expression is a piece of code that can be evaluated to produce a value.

\\n\\n

Example

\\n\\n
void 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\\n

Statements

\\n\\n

A statement is an instruction that performs an action; it does not produce a value.

\\n\\n

Example

\\n\\n
void 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\\n

Output:

\\n\\n
TUTORIAL\\nName length greater than 3\\n
\\n\\n

In short: expressions produce values, statements perform actions.

\\n\\n

You can use expressions where a value is needed, but you cannot use statements.

\\n\\n
\\n

A 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
\\n\\n

Identifier Naming Conventions

\\n\\n

An identifier is the name you give to variables, functions, classes, and other code elements.

\\n\\n

Dart has rules and conventions for identifiers:

\\n\\n

Naming 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
\\n\\n

Naming Conventions (Recommended)

\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
Code ElementNaming StyleExamples
Class namesUpperCamelCaseStudentInfo, HttpClient
Enum typesUpperCamelCaseColor, Status
Library names, file nameslowercase_with_underscoresmy_utils, user_service
Variable nameslowerCamelCaseuserName, maxCount
Function nameslowerCamelCasegetUserInfo(), calculateTotal()
Constant nameslowerCamelCasepiValue, defaultPort
Private membersLeading underscore_internalState, _privateMethod()
\\n\\n

Example

\\n\\n

Practical 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\\n

Output:

\\n\\n
Username: tutorial\\nMaximum login attempts: 3\\n
\\n\\n
\\n

In 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
\\n\\n

Image 1

← Dart OperatorsDart Install β†’