Variables and types in Dart

Suragch
5 min readOct 24, 2020

For people new to programming

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…

--

--