Dart Basic Syntax


Understanding the basic syntax and structure of Dart is essential for writing and organizing code. Dart syntax is clean, simple, and inspired by languages like JavaScript, Java, and C++, making it easy to learn, especially if you're familiar with other object-oriented languages.

Here’s a rundown of the core components of Dart’s syntax and structure:


1. Main Function

The main() function is the entry point of every Dart program. Dart will look for this function to start execution.

void main() { print("Hello, Dart!"); }
  • void: Indicates that main() does not return a value.
  • print: A built-in function that outputs text to the console.

2. Variables and Data Types

Dart supports both type inference and explicit typing. To declare variables, use either var, final, or const.

void main() { // Using `var` (type is inferred) var name = 'Alice'; // Explicit typing int age = 25; double height = 5.7; String city = 'New York'; // Constants final country = 'USA'; // Value can't be changed const pi = 3.14159; // Compile-time constant }
  • var: The type is inferred based on the assigned value.
  • final: Used for variables whose values won’t change after initialization.
  • const: Used for compile-time constants that never change.

3. Functions

Functions are reusable blocks of code. Dart supports both named parameters and positional parameters.

int add(int a, int b) { return a + b; } void main() { print(add(5, 3)); // Output: 8 }
  • Optional Named Parameters:

    void greet({String name = 'Guest'}) { print('Hello, $name!'); }
  • Optional Positional Parameters:

    void greet([String name = 'Guest']) { print('Hello, $name!'); }

4. Control Flow Statements

Dart provides the typical control flow statements such as if, else, for, while, and switch.

void main() { int number = 5; // if-else if (number > 0) { print("Positive"); } else { print("Non-positive"); } // for loop for (int i = 0; i < 5; i++) { print(i); } // while loop int count = 0; while (count < 3) { print(count); count++; } // switch-case var grade = 'A'; switch (grade) { case 'A': print('Excellent'); break; case 'B': print('Good'); break; default: print('Try harder'); } }

5. Collections: Lists, Sets, and Maps

Collections are used to store multiple values in Dart. The primary types are List, Set, and Map.

void main() { // List List<int> numbers = [1, 2, 3]; print(numbers[0]); // Output: 1 // Set Set<String> fruits = {'apple', 'banana', 'apple'}; print(fruits); // Output: {apple, banana} (unique values only) // Map Map<String, int> ages = {'Alice': 25, 'Bob': 30}; print(ages['Alice']); // Output: 25 }

6. Classes and Objects

Dart is an object-oriented language that supports classes and objects. Classes are blueprints for creating objects.

class Person { String name; int age; // Constructor Person(this.name, this.age); // Method void introduce() { print('Hello, I am $name and I am $age years old.'); } } void main() { var person = Person('Alice', 25); person.introduce(); // Output: Hello, I am Alice and I am 25 years old. }

7. Comments

Comments are used to add explanations or to disable code temporarily.

// Single-line comment /* Multi-line comment */ /// Documentation comment for classes, methods, and functions void main() { print("Hello, Dart!"); // Prints a message }

8. String Interpolation

Dart uses the ${variable} syntax for string interpolation. If the variable is a simple identifier, you can omit the curly braces.

void main() { String name = 'Dart'; print('Hello, $name!'); // Output: Hello, Dart! print('2 + 2 is ${2 + 2}'); // Output: 2 + 2 is 4 }

9. Arrow Functions

For concise functions, you can use arrow syntax (=>) for single-line functions.

int square(int x) => x * x; void main() { print(square(4)); // Output: 16 }

Summary of Dart Syntax

  • The main function is the entry point.
  • Variables can be declared with var, final, or const.
  • Functions support named and positional parameters.
  • Control structures include if-else, for, while, and switch.
  • Dart collections include List, Set, and Map.
  • Classes define objects with fields and methods.
  • String interpolation and arrow functions allow for more concise code.

These components provide the fundamental structure and syntax for writing effective Dart code.