Variables and types in Dart

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.


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.


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.


There are different types of values in Dart.


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;


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.


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.


Flutter and Dart developer. Twitter: @suragch1