No history yet

Dart Language Basics

The Anatomy of a Dart Program

Every Dart program needs a starting point. Think of it like the front door to a house. For Dart, this entry point is always a special function called main. When you run your code, Dart looks for main and begins executing whatever instructions it finds inside.

void main() {
  // Your code goes here
}

The void part simply means this function doesn't return any value. The parentheses () are where you could put inputs for your program, but we'll leave them empty for now. The curly braces {} enclose the body of the function, which is where the magic happens.

Every line of code that gives an instruction in Dart must end with a semicolon ;.

The most basic instruction you can give is to print something to the console. The console is a text-based window where your program can display messages. To do this, you use the print() function. Let's try the classic "Hello, World!" program.

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

Storing Information

Programs need to remember things, like a score in a game or a user's name. We store this information in variables, which are like labeled boxes for holding data.

variable

noun

A named container in a computer's memory that holds a value. The value can change during program execution.

When you create a variable, you need to give it a name and a value. You can let Dart figure out the data type for you by using the var keyword. This is called type inference.

// Dart infers that 'name' is a String and 'age' is an int.
var name = 'Alex';
var age = 25;

Alternatively, you can be explicit about the type of data your variable will hold. Dart has several basic, or primitive, data types:

  • int: For whole numbers, like 10 or -5.
  • double: For numbers with decimal points, like 3.14 or -0.01.
  • String: For text, enclosed in single or double quotes, like 'Hello' or "Dart".
  • bool: For true or false values, written simply as true or false.
TypeDescriptionExample
intIntegers (whole numbers)int score = 100;
doubleFloating-point numbersdouble price = 9.99;
StringA sequence of charactersString message = 'Game Over';
boolBoolean value (true/false)bool isLoggedIn = true;

Sometimes you need a variable that won't change. Dart gives you two options for this: final and const. A final variable can only be set once. A const variable is even stricter; it's a compile-time constant, meaning its value must be known before the program even runs. For now, you can think of them as ways to create unchangeable variables.

void main() {
  final String username = 'playerOne';
  const double pi = 3.14159;

  print(username);
  print(pi);
}

Making Things Happen

Simply storing data isn't very useful. We need to operate on it. Dart provides operators to perform arithmetic and make logical comparisons.

Arithmetic operators work just like they do in math class. You can add (+), subtract (-), multiply (*), and divide (/).

void main() {
  int a = 10;
  int b = 3;

  print(a + b); // Output: 13
  print(a - b); // Output: 7
  print(a * b); // Output: 30
  print(a / b); // Output: 3.333...
}

Logical operators help your program make decisions. They evaluate conditions and result in a boolean value (true or false).

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

You can combine these to form more complex expressions. For example, let's see if a player's score is high enough to win.

void main() {
  int score = 120;
  int winningScore = 100;

  bool didWin = score >= winningScore;

  print('Did the player win?');
  print(didWin); // Output: true
}

With these building blocks—the main function, variables, basic types, and operators—you have the foundation for writing any Dart program. Every complex app, no matter how large, is built upon these simple concepts.

Quiz Questions 1/6

What is the name of the special function that serves as the entry point for every Dart program?

Quiz Questions 2/6

Which data type is used to store text, like a person's name?