Values and variables

on Saturday, 18th of July, 2020

The basic building block of information in your Dart program will be variables. Anytime you're working with data in an app, you can store that data in variables. For example, if you're building a chat application, you can use variables to refer to chat messages or a user.

Variables store references to these values.

Defining Variables

One of many ways, and the simplest way, to define a variable in Dart is using the var key word.

var message = 'Hello, World';

This example creates a variable called message, and also initializes the variable with a String value of Hello, World. Now, you can access that value by referring to the message variable.

var message = 'Hello, World';

// => Hello, World

Inferring the type

Dart is a typed language. The type of the variable message is String. Dart can infer this type, so you did't have to explicitly define it as a String. Importantly, this variable must be a String forever. You cannot re-assign the variable as an integer. If you did want to create a variable that's more dynamic, you'd use the dynamic keyword. We'll see examples of that in a later lesson.

dynamic message = 'Hello World';

You can safely re-assign this variable to an integer.

dynamic message = 'Hello, World';
message = 8; 

NB: It's rarely advisable to use dynamic. Your code will benefit from being type safe.

Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action