No history yet

Introduction to Dart

Meet Dart

Every Flutter app is built using a programming language called Dart. Think of Flutter as the toolkit for building the house, and Dart as the language of the blueprints. It’s a modern, flexible language developed by Google, designed to be easy to learn, especially if you have experience with languages like JavaScript, Java, or C++.

Like many programs, every Dart application starts from one special place: the main() function. This is the entry point, the first piece of code that runs when your app launches. Let's look at the classic first program.

// The main function is the starting point for all Dart apps.
void main() {
  // The print() function displays text in the console.
  print('Hello, World!');
}

Notice a few things here. The // creates a comment, which is a note for humans that the computer ignores. Every instruction, or statement, ends with a semicolon (;). This tells Dart you've finished a command.

Variables and Data Types

Programs work by storing and manipulating information. We store this information in variables. A variable is just a named container for a piece of data. Dart is a statically typed language, which means every variable has a type that's known when the code is compiled.

While you can explicitly declare a variable's type, Dart is smart enough to figure it out on its own if you use the var keyword. Here are the most common types you'll encounter:

TypeDescriptionExample
intIntegers, or whole numbers.int score = 10;
doubleFloating-point numbers (with decimals).double price = 19.99;
StringA sequence of characters, or text.String name = "Alice";
boolA boolean value, either true or false.bool isLoggedIn = true;

Here's how you might use them in practice:

void main() {
  // Dart can infer the type with `var`.
  var greeting = 'Welcome to Dart!';
  var year = 2024;

  // Or you can declare the type explicitly.
  String language = 'Dart';
  int version = 3;
  bool isAwesome = true;

  print('$language version $version is awesome: $isAwesome');
}

Sometimes you have a variable that shouldn't be changed. For this, you can use final or const. Use final for a variable whose value is set once and never changes. Use const for a variable whose value is known at compile time (before the program even runs).

Use final for values that won't change after they're first calculated. Use const for values that are always, unchangingly the same.

Controlling the Flow

Your code doesn't just run from top to bottom. You need ways to make decisions and repeat actions. That's where control flow statements come in.

To make a decision, you use an if statement. It checks if a condition is true and runs a block of code if it is. You can also provide an else block to run if the condition is false.

void main() {
  int score = 85;

  if (score >= 60) {
    print('You passed!');
  } else {
    print('Try again.');
  }
}

To repeat an action a specific number of times, a for loop is your best friend. It has three parts: an initializer, a condition to continue, and an action after each loop.

// This loop prints numbers from 1 to 5.
for (int i = 1; i <= 5; i++) {
  print(i);
}

Functions

As programs grow, you'll want to organize your code into reusable blocks. That's what functions are for. A function is a named block of code that performs a specific task. We've already been using one: main()!

A function can take inputs, called parameters, and can return an output. You define the type of data the function will return (or void if it returns nothing), followed by the function name and its parameters in parentheses.

// This function takes a name and returns a greeting.
String greet(String name) {
  return 'Hello, $name!';
}

void main() {
  String message = greet('Maria');
  print(message); // Prints 'Hello, Maria!'
}

Object-Oriented Basics

Dart is an object-oriented programming (OOP) language. This means you can bundle data and the functions that operate on that data into a single unit called an object. The blueprint for an object is called a class.

A class defines properties (variables) and methods (functions) for a concept. For example, we could create a Dog class. It might have properties like name and age, and a method like bark().

class Dog {
  // Properties
  String name;
  int age;

  // A special function called a constructor
  Dog(this.name, this.age);

  // Method
  void bark() {
    print('$name says: Woof!');
  }
}

Once you have the class (the blueprint), you can create specific instances of it, which are the objects.

void main() {
  // Create an instance of the Dog class
  var myDog = Dog('Fido', 3);

  // Access its properties and call its methods
  print('${myDog.name} is ${myDog.age} years old.');
  myDog.bark(); // Fido says: Woof!
}

This is just a quick tour of Dart's core concepts. By combining variables, control flow, functions, and classes, you can build the logic for any application you can imagine.

Quiz Questions 1/6

What is the primary purpose of the main() function in a Dart application?

Quiz Questions 2/6

Dart is a statically typed language. What does this mean?