Learn Dart in 5 minutes

Learn Dart in 5 minutes

Variables, types, operators, control flow, functions, and classes.

Do you want to learn Flutter or just interested in Dart? Let’s do a 5 minute run-through of the Dart language to get you up to speed!

shipit

We are going to cover variables, types, operators, control flow, functions, and classes, so let’s dive in.

Variables

Variables are used to store information in memory using a keyword that you define. The information stored can be of many different types. For example, numbers.

final x = 10;

Types

Numbers

In Dart, there are two types of numbers: int and double. Integers are whole numbers, while doubles can be fractions or decimals.

int x = 10;
double y = 10.0;

Strings

There are also strings. A string is a sequence of characters used to represent text in programming. For example, “Hello, world!” is a string.

String x = "Hello, world!";

And technically, numbers can be strings too, but the difference with the string data type is that you won’t be able to add those numbers since a string is the character representation of that number, not the number itself. Well, technically you can still add them, but it would just put the characters next to each other. This is called concatenation.

String first = "4";
String second = "5";
print(first + second); // Output: "45"

Booleans

Then there is the boolean type, which represents either true or false.

bool x = true;
bool y = false;

List

Next, we have the list data type, which can also be called arrays. A list is exactly how it sounds: it can hold a list of other data types.

List<String> fruits = ['Apple', 'Banana', 'Orange'];

Including a list of lists if you want to get crazy.

List<List<int>> nestedList = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
int value = nestedList[1][2]; // 6

Map

Next is a data type called map, which is very similar to a list except that each item in a map has a key-value pair. In this example, the key is the name of a person and the value is their age. You can retrieve the age of each person by using the key of that key-value pair.

Map<String, int> ages = {
'Tadas': 28,
'Robert': 28,
'Your Mom': 69,
};
print(ages['Tadas']); // Output: 28

Others

The last data type is functions, but we will cover functions later in this article.

These are the data types you will use 99% of the time.

However, there are a few others like records, sets, and runes. You really don’t need to worry about these, but for the sake of completeness:

  • Records are like two previously mentioned data types bundled into one.
final (name, age, job) = ('John', 25, 'Engineer');
print(name); // Output: John
  • Sets are like lists, but each item has to be unique.
var fruits = {'Apple', 'Banana', 'Orange'};
fruits.add('Mango');
print(fruits); // Output: {Apple, Banana, Orange, Mango}
  • And runes are a datatype commonly used for emojis.
var str = 'Hello, 🌍!';
print(str.runes); // Output: (72, 101, 108, 108, 111, 44, 32, 127757, 33)

Operators

Next, we move onto operators in Dart. Operators are used to perform logic on your data. You can:

  • + - Add and subtract.
  • * / Multiply and divide.
  • % Get the remainder of a division.
  • > < Check the relationship of data using greater than or less than.
  • == Check if two values are equal to each other.
  • || && There are logical OR and AND operators.
  • ?? The if-null operator checks if the first value is null it will use the second value.
  • ? : Conditionals check the output of the first expression and if it’s true use the second, if false use the third.
  • = And lastly, there are assignment operators which you use to assign values to a variable.

Here is a complete list of operators in Dart, but the ones we discussed are most commonly used.

Xnapper-2025-03-16-12.06.59

Control Flow

Next is control flow statements. Just like the name sounds, these control the execution flow of your application.

If-else

Let’s start with if-else statements. These check if a condition is true. If it is, execute the logic within the first bracket pair, otherwise execute logic from the second bracket pair.

final temperature = 30;
if (temperature > 25) {
print('Hot day.');
} else {
print('Cool day.');
}

For loops

There are also for loops. This can either loop through a list of objects, or define your own loop.

for (var i = 0; i < 5; i++) {
print(i); // Output: 0,1,2,3,4
}

While loops

There are while loops which are similar to for loops but you do the incrementing within the loop.

var i = 0;
while (i < 5) {
print(i); // Output: 0,1,2,3,4
i++;
}

Switch case

And lastly, switch case statements which are similar to if-else statements but they are nicer to use when you have a lot of conditionals.

var color = 'red';
switch (color) {
case 'red':
print('Color is red');
break;
case 'blue':
print('Color is blue');
break;
default:
print('Unknown color');
}

Functions

We mentioned a variable type called functions. But it is not just a variable type. Functions are a way to organize your code into sections to make your application code more readable. A function takes defined inputs and returns an output. For example, the print function takes an input and displays it in your terminal.

for (var i = 0; i < 5; i++) {
print(i); // Output: 0,1,2,3,4
}

Another example is a function to calculate tips. Here is what the function might look like and instead of having to write out the equation every time, you can use this calculate tip function.

double calculateTip(double bill) {
double tip = bill * 0.20;
return tip;
}
double bill = 50.0; // Example bill amount
double tip = calculateTip(bill);
print(tip.toStringAsFixed(2)); // Output: 10.00

In Dart, functions can also be variables. So you can assign a function to a memory location, or even pass in a function to another function.

void executeFunction(void Function() action) {
print("Before executing function...");
action(); // Call the passed function
print("After executing function...");
}
void sayHello() {
print("Hello, Dart!");
}
void main() {
executeFunction(sayHello);
}

Asynchronous Programming

These functions can run synchronously or asynchronously. This is related to the execution order of the code. Synchronous code runs in a row. That means each line of code gets executed in order.

synchronous

Asynchronous code doesn’t always get executed in order.

asynchronous

This is because it relies on information outside the execution loop.

The most common example of this is when you call a database. If a database call was performed synchronously, the entire application would come to a halt, until the information from the database was retrieved.

With asynchronous programming, your application can keep running until the information comes back from the database, and then it gets processed.

giphy

This type of data is called Futures.

Dart also has Streams, which is data that is streamed into your application.

To be clear, we used a database as an example, but futures and streams can be used internally within an application.

Classes

The last thing we will cover in Dart is classes. Classes are an object-oriented programming principle that separates your code into objects.

class Person {
Person(this.name);
String name;
void greet() => print('Hello, I am $name!');
}
void main() {
final person = Person('Mom');
person.greet(); // prints "Hello, I am Mom!"
}

This is a layer that adds even more separation than functions. A class is the definition of the code, but when it is used within an app it becomes an object. Each object can have its own variables and functions that are tied to that class.

class Car {
Car(this.model);
String model;
void drive() => print('$model is driving!');
}
void main() {
final car1 = Car('Tesla');
final car2 = Car('BMW');
car1.drive(); // Tesla is driving!
car2.drive(); // BMW is driving!
}

This lets you separate your code into more simple, reusable components, making it easier to manage and scale. Instead of working with scattered variables and functions, you can group related data and behavior into a class.

For example, instead of having separate variables for a car’s speed and color, along with standalone functions to accelerate or brake, you can define a Car class that encapsulates these properties and behaviors:

class Car {
Car(this.color, this.speed);
String color;
int speed;
void accelerate() {
speed += 10;
print("The car is now going $speed mph.");
}
}
void main() {
Car myCar = Car("Red", 0);
myCar.accelerate(); // The car is now going 10 mph.
}

Want to dive deeper into Dart, I think you will find this next article on working with Dart exceptions interesting.