flutter
/

Dart Syntax – Writing Your First Code

Last Sync: Today

On this page

11
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

flutter

Dart Syntax – Writing Your First Code

Dart Syntax – The Basics

Syntax is the set of rules that defines how a Dart program is written. Just like grammar in human language, syntax ensures that your code is structured correctly so the Dart compiler can understand and execute it. This guide covers the fundamental syntax elements every Dart developer must know.

  1. Statements and Semicolons

In Dart, each instruction is called a statement. Statements must end with a semicolon (;). Forgetting a semicolon is a common syntax error.

DARTRead-only
1
void main() {
  print('Hello, Dart!'); // statement ends with ;
  var name = 'Alice';    // another statement
}

  1. Comments

Comments are ignored by the compiler and are used to explain code. Dart supports three types of comments:

    • Single‑line comments start with // and continue to the end of the line.
    • Multi‑line comments are enclosed between /* and */.
    • Documentation comments start with /// or /** and are used by tools like dart doc.
DARTRead-only
1
// This is a single‑line comment

/* This is a
   multi‑line comment */

/// This is a documentation comment for a function.
void greet() { }

  1. The main() Function

Every Dart program must have a main() function. It serves as the entry point – the first code that runs when you execute the program. The main() function can be written with or without a return type (void means it returns nothing).

DARTRead-only
1
void main() {
  // program starts here
}

// Alternative (less common)
main() {
  // also valid
}

  1. Printing to the Console

The print() function outputs text to the console. It's useful for debugging and displaying results.

DARTRead-only
1
void main() {
  print('Hello, world!');
  print(42);
  print(3.14);
}

  1. Case Sensitivity

Dart is case‑sensitive. That means myVariable, MyVariable, and MYVARIABLE are all different names. Always use consistent casing to avoid errors.

DARTRead-only
1
void main() {
  var myVar = 10;
  var myvar = 20; // different variable
  print(myVar);  // 10
  print(myvar);  // 20
}

  1. Code Blocks and Indentation

Code blocks are enclosed in curly braces {}. They group multiple statements together, for example in functions, loops, or conditionals. Dart uses braces, not indentation, to define blocks, but consistent indentation is strongly recommended for readability.

DARTRead-only
1
void main() {
  if (true) {  // start of block
    print('Inside if');
  }            // end of block
}

  1. Keywords

Dart has a set of reserved words (keywords) that have special meaning. You cannot use them as identifiers (variable names, function names, etc.). Examples: if, else, for, while, class, void, int, String, true, false, null, var, final, const.

  1. Identifiers

Identifiers are names given to variables, functions, classes, etc. Rules:

    • Can contain letters, digits, underscores (_), and dollar signs ($).
    • Cannot start with a digit.
    • Cannot be a keyword.
    • By convention, variable and function names use camelCase (e.g., myVariable), and class names use PascalCase (e.g., MyClass).

Complete Example

DARTRead-only
1
// This program demonstrates basic Dart syntax

void main() {
  // Print a greeting
  print('Welcome to Dart!');
  
  // Declare variables
  var name = 'Dart Learner';
  int year = 2025;
  
  // Use a conditional
  if (year > 2020) {
    print('Hello, $name!');
  }
}

Key Takeaways

    • Every statement ends with a semicolon.
    • Use comments to document your code.
    • Every program needs a main() function.
    • Dart is case‑sensitive.
    • Code blocks are enclosed in {}.
    • Follow naming conventions for readability.

Try it yourself

void main() {
  // Write your Dart code here
  print('Learning Dart syntax!');
}

Test Your Knowledge

Q1
of 4

What must every Dart program have as an entry point?

A
init() function
B
start() function
C
main() function
D
run() function
Q2
of 4

Which of the following is a valid single‑line comment in Dart?

A
/* comment */
B
// comment
C
# comment
D
<!-- comment -->
Q3
of 4

What is the output of this code? void main() { var x = 5; var X = 10; print(x); }

A
10
B
5
C
Error: case sensitivity
D
null
Q4
of 4

Which of the following is NOT a valid identifier in Dart?

A
_myVar
B
$value
C
2ndPlace
D
camelCase

Frequently Asked Questions

What happens if I forget a semicolon in Dart?

Forgetting a semicolon results in a compile‑time error. The Dart compiler will point to the line where the semicolon is missing. Always end each statement with ;.

Can I use multi‑line comments inside other comments?

No, multi‑line comments (/* ... */) cannot be nested. If you need to comment out a block that already contains a multi‑line comment, use line comments (//) for the outer block or restructure your code.

Is the `main()` function always required?

Yes, every Dart program must have a main() function as the entry point. For web or Flutter apps, the framework may call main() for you, but you still need to define it.

What is the difference between `print()` and `stdout.write()`?

print() adds a newline after the output and is the simplest way to display text. stdout.write() (from dart:io) writes without a newline, giving you more control over formatting.

Are Dart keywords reserved words that can never be used as identifiers?

Yes, keywords like if, class, void cannot be used as identifiers. However, some words like show, hide, on are contextual keywords and may be allowed in specific contexts. It's best to avoid all keywords for clarity.

Can I use spaces in variable names?

No, variable names cannot contain spaces. Use camelCase (myVariableName) or underscores (my_variable_name) to separate words.

Is Dart case‑sensitive?

Yes, Dart is case‑sensitive. myVar, myvar, and MyVar are three different identifiers. Be consistent to avoid bugs.

What is the difference between `void main()` and `main()`?

Both are valid, but explicitly writing void main() is considered good practice because it clearly indicates that the function returns nothing. Omitting the return type makes it dynamic by default, which is less clear.

Previous

dart installation

Next

dart variables

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.