Dart Programming Language

Basic Syntax

main() function

Dart program starts from main():

void main() {
  print('Hello, World!');
}

Statements in dart end with semicolon

int age = 17;
print(age);

Comments Example

// Single line

/*
 Multi-line
 comment
*/

Variables And Data Types

var name = "Manan";   // Dart guesses it's a String
String city = "Dhaka";
int count = 10;
double rate = 3.14;
bool isActive = true;
dynamic isChild = false // You can use any variable type such as 'string', 'int', 'bool'
Var name = 'Manan'

late keyword

Late Keyword tells Dart you’ll assign the value later.

late String username;
username = "Manan";

const vs final

  • final → assigned once at runtime
  • const → compile-time constant
final timeNow = DateTime.now();  // OK
const pi = 3.1416;               // Must be known before runtime

Null Variable

In Dart, to introduce a null value, a variable must be explicitly declared as nullable. This is achieved by appending a question mark (?) after the variable’s type.

// Declaring a nullable String variable
String? name; 

// Declaring a nullable integer variable
int? age; 

// Assigning null to nullable variables
name = null;
age = null;

// You can also assign a value of the declared type
name = "Alice";
age = 30;

// Correct syntax to print null value
print(name?.length)

Strings

Basic String Example:

String s1 = "Hello";
String s2 = 'Dart';

Multiline String Example:

String longText = """
This is a long text block.
""";

Interpolation String Example:

String name = "Manan";
print("Hello $name");
print("Hello ${name.length}"); // Also correct syntax
print("Next year you’ll be ${age + 1}");

Some Useful methods for strings

print(name.toUpperCase());
print(name.contains("Ma"));
print(name.substring(0, 2));

If/Else Condition

Use if when you want to run code only if a condition is true. Syntax:

if (condition) {
  // code runs if condition is true
} else if (anotherCondition) {
  // runs if previous false & this true
} else {
  // runs if all above are false
}

Example:

int age = 17;

if (age >= 18) {
  print("You are an adult");
} else if (age == 17) {
  print("Almost adult");
} else {
  print("You are a teenager");
}

Ternary Operator (Short If)

Used when you need a quick one-line if/else. Syntax & Example:

condition ? valueIfTrue : valueIfFalse;
String result = age >= 18 ? "Adult" : "Teen";

SWITCH / CASE

Use when you have multiple fixed choices. Syntax:

switch (variable) {
  case value1:
    // code
    break;

  case value2:
    // code
    break;       // break is needed for empty condition

  default:
    // code
}

Example:

String day = "Friday";

switch(day) {
  case "Monday":
    print("Start of week");
    break;

  case "Friday":
    print("Weekend starts!");
    break;

  default:
    print("Normal day");
}

FOR Loop

Used when you know how many times to repeat. Syntax:

for (initialization; condition; increment) {
  // repeated code
}

Example:

for (int i = 1; i <= 5; i++) {
  print("Number $i");
}

FOR-IN Loop

Used to loop through items in a list, set, map keys etc. Syntax & Example:

for (var item in collection) {
  // use item
}

// Example
List<String> fruits = ["Mango", "Banana", "Apple"];

for (var f in fruits) {
  print(f);
}

WHILE Loop

Runs while a condition is true. Syntax:

while (condition) {
  // repeat
}

// Example
int count = 1;

while (count <= 3) {
  print("Count $count");
  count++;
}