No history yet

Introduction to Dart

The Language of Flutter

Before you can build amazing apps with Flutter, you need to learn the language it's built on: Dart. Created by Google, Dart is designed to be a client-optimized language for building fast apps on any platform. Think of it as the grammar and vocabulary you'll use to tell your app what to do.

Dart is a modern, object-oriented language. If you've ever seen JavaScript, Java, or C++, some parts will look familiar. But Dart has its own unique features that make it perfect for creating the smooth, responsive user interfaces Flutter is known for. Let's start with the basics.

Your First Dart Program

Every Dart application starts its journey in the same place: a special function called main(). A function is just a named block of code that performs a specific task. The main() function is the entry point, the very first thing your program runs.

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

Let's break this down. The word void before main() means this function doesn't return any value when it's done. The parentheses () are where you could pass information into the function, but main usually doesn't need any. The code to be executed lives inside the curly braces {}.

Inside, print() is a built-in Dart function that writes text to the console. The text itself, 'Hello, World!', is a String and must be enclosed in single or double quotes. Finally, notice the semicolon ; at the end of the line. In Dart, semicolons are used to mark the end of a statement, like a period at the end of a sentence.

Storing Information

Programs need to keep track of information, like a user's name, a score in a game, or whether a setting is turned on. We store this data in variables.

variable

noun

A named container for storing a piece of data that can be referenced and manipulated in a program.

Dart is a type-safe language. This means every variable must have a specific type, and Dart will make sure you don't accidentally put the wrong kind of data in it. Here are the most common types you'll use:

  • int: For integers, or whole numbers (e.g., 10, -5, 0).
  • double: For floating-point numbers, or numbers with a decimal point (e.g., 3.14, -0.01).
  • String: For sequences of characters, or text. Use single ' ' or double " " quotes.
  • bool: For boolean values, which can only be true or false.
// Declaring variables with their types.
int score = 100;
double temperature = 98.6;
String greeting = 'Hello there';
bool isDartFun = true;

// You can also use 'var' and let Dart figure out the type.
var playerName = 'Alice'; // Dart infers this is a String.

// Use 'final' for variables that will not be reassigned.
final String appName = 'My Awesome App';

Using var is a convenient way to declare a variable when its type is obvious from the value you're giving it. If you have a variable that won't change after it's first assigned, it's good practice to declare it with final. This can make your code safer and easier to understand.

Making Decisions and Repeating Actions

A program that only runs from top to bottom isn't very smart. To create dynamic apps, we need code that can make decisions and perform repetitive tasks. This is handled by control flow statements.

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.

int userAge = 19;

if (userAge >= 18) {
  print('You are eligible to vote.');
} else {
  print('You are not yet eligible.');
}

For repeating actions, we use loops. The most common is the for loop, which is great when you know exactly how many times you want to repeat something. It has three parts: an initializer (where you start), a condition (when you stop), and an incrementer (how you count up).

// This loop will print numbers 0 through 4.
for (int i = 0; i < 5; i++) {
  print('The number is $i');
}

The $i syntax inside the string is called string interpolation. It's a handy shortcut for inserting the value of a variable directly into a string.

Organizing Code with Functions

As your programs grow, you'll want to organize your code into reusable pieces. We do this with functions. We already met main() and print(), but you can easily create your own.

A function can take in data through its parameters and can send data back using a return statement. You must specify the type of data the function will return (or void if it returns nothing).

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

void main() {
  // Call the function and store its result in a variable.
  String myGreeting = createGreeting('Bob');
  print(myGreeting); // Prints 'Hello, Bob!'
}

Here, createGreeting has one parameter, name, which must be a String. It uses this parameter to build a new string and then returns it. Back in main(), we call the function and capture its output in the myGreeting variable.

An Introduction to Objects

Dart is an object-oriented programming (OOP) language. This means we can model real-world things as objects in our code. An object is a bundle of related data (properties) and functions (methods).

The blueprint for creating an object is called a class. A class defines what properties and methods an object of that type will have.

// This is the blueprint for a Player.
class Player {
  // Properties
  String name;
  int score = 0;

  // Constructor - a special method for creating objects.
  Player(this.name);

  // Method
  void increaseScore(int points) {
    score = score + points;
    print('$name now has a score of $score.');
  }
}

void main() {
  // Create a new Player object from the class.
  Player playerOne = Player('Alice');

  // Call the object's method.
  playerOne.increaseScore(10);
}

In this example, the Player class defines that every player will have a name and a score. The Player(this.name) line is a constructor, a special function that runs when we create a new Player object. It's a shortcut for assigning the incoming name to the object's name property.

The increaseScore function is a method. It belongs to the Player class and can access the object's own properties, like name and score.

In main(), we create an instance of the Player class called playerOne. Then, we can use dot notation (playerOne.increaseScore()) to access its methods. This is the core idea of object-oriented programming, and it's fundamental to how Flutter apps are built.

Now that you've seen the fundamentals of Dart, let's test your knowledge.

Quiz Questions 1/6

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

Quiz Questions 2/6

Which Dart data type is used to represent a whole number, like 42 or -100?

These are the building blocks you'll use every day when writing Dart code. With this foundation, you're ready to see how these concepts are used to build beautiful Flutter applications.