Variables and types in Dart

For people new to programming

Image for post
Image for post

Recently I’ve been teaching a class about Flutter, so this article is an introduction to Dart with a few practice exercises.

Variables

A variable is a name that can hold a value. Here’s an example:

var x = 1;

The variable name is x and it equals 1. The semicolon ; means it’s the end of the statement.

The var keyword means that x is variable. You can change it.

var x = 1;
x = 5;

Now x equals 5.

Constants

If you have a variable that won’t change, you can make it a constant:

const a = 3;

The const keyword means it’s a constant. You can’t change it.

Types

There are different types of values in Dart.

int

In the examples above, the values were whole numbers (integers). Dart calls this type int and you could use int instead of var:

int x = 1;
x = 5;

Dart knows that 1 is of type int, though, so normally you don’t need to write the type. The only reason I’m writing the type is so that you can see it. You can just use var if you need a variable or const if you need a constant:

var x = 1;
const a = 3;

double

The double type is for decimal numbers:

double y = 2.133836;

or you can write it like this:

var y = 2.133836;
const b = 0.001;

You can do math on numbers:

var x = 2;
var y = 3.5;
var z = x + y;

z equals 5.5.

String

The String type is for storing text values:

String name = 'Mary';

Or you can write it like this:

var name = 'Mary';
const city = 'Paris';

A string value should have single ' or double " quote marks around it.

You can add variables inside of a string. Use a $ dollar sign before the variable name:

var name = 'Mary';
const age = 12;
var sentence = '$name is $age.';

Now sentence has the following value:

Mary is 12.

bool

The bool type is for Boolean values, that is, true or false.

bool isSunny = true;

Or you can write it like this:

var isSunny = true;

Or this:

const isSunny = true;

When you have two words in a variable name, like isSunny, you should start the second word with a capital letter. This is called camel case because the capital letter looks like a camel’s hump.

List

The List type can hold a list of values of another type. Here is a list of strings:

List<String> animals = ['horse', 'camel', 'cow', 'sheep', 'goat'];

You use < > angle brackets to say what type of list it is, so <String> means it’s a list of strings. To make a list you use [ ] square brackets. Add commas to separate the things inside the list.

As in the other examples, you can use var or const:

var animals = ['horse', 'camel', 'cow', 'sheep', 'goat'];
const numbers = [3, 51, 33, 8, 7];

Map

The Map type is like a list of variables. Each variable has a name (called a key) and a value. Here is an example:

Map<String, int> ages = {'Bob': 36, 'Mary': 12, 'Susan': 97, 'Paul': 75};

The <String, int> part means the key is a string and the value is an integer.

It’s a little hard to read a map when the elements of the map are in a long line, so you can also write it like this:

var ages = {
'Bob': 36,
'Mary': 12,
'Susan': 97,
'Paul': 75,
};

Now it’s easy to see that each key-value pair is separated by commas. There is also a : colon between the key and the value. The meaning is that Bob is 36 years old, Mary is 12 years old, and so on.

Custom types

The Dart types above are all useful, but you can also make your own types.

enum

An enum is a way to make your own list of options. It’s like bool but with more than two options. You can create an enum for the seasons like this:

enum Seasons {
spring, summer, fall, winter
}

Then you use it like this:

var seasonNow = Seasons.fall;

class

With a class you can combine simple types together into a more complex type. Here is an example for a custom type that contains information about the chapter of a book:

class Chapter {
Chapter(this.number, this.text);
final int number;
final String text;
}

Notes:

  • This class combines an int and a String.
  • The second line is called a constructor. To construct a value with this class you have to give it a number and some text. The this keyword means, use the number and text variable names in this class.
  • The final keyword is like const. It’s a variable that can’t be changed. However, you can only use const when you know the value before your run the program. In the Chapter class, the class doesn’t know what number and text will be until after you run the program.

Now you can use it like this:

var chapter = Chapter(1, 'Once upon a time...');

Preparing to do the exercises

Now it’s time to practice.

Setup

Open DartPad, an online Dart editor.

Example problem

Create a variable called x and give it a value of 1. Then change the value to 5.

Solution:

You start with the main function:

void main() {

}

All Dart programs start inside the main function, that is, inside the { } curly braces after void main().

Write your answer inside the function:

void main() {
var x = 1;
x = 5;
}

If you run this now you won’t see anything, so add a print statement:

void main() {
var x = 1;
x = 5;
print(x);
}

Now press the RUN button and you’ll see 5 printed in the console.

Exercises

Complete the following tasks:

  • Create a constant named pi and give it a value of 3.14. Create another constant named r and give it a value of 2. Then create a constant named area and give it the value of pi times r times r. (Hint: the multiplication symbol in Dart is *.)
  • Create a string variable named favoriteFood and give it a value. Then create another string variable named sentence whose value is a sentence saying what your favorite food it.
  • Create a Boolean variable named isDoorOpen and give it a value based on whether the closest door to you is open or not.
  • Create a list of country names.
  • Create a map where the keys are country names and the values are the capital city names.
  • Create an enum for the days of the week. (Hint: the enum definition needs to go outside of the main function.)
  • Create a class named Country to group information like name, capital, and population. (Hint: the class definition needs to go outside of the main function.)

When you’re finished, you can check your answers here.

A Flutter and Dart developer. Follow me on Twitter @suragch1 to get updates of new articles.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store