Search⌘ K
AI Features

Storing Data with Variables

Explore how to store data using variables in Dart by learning declaration syntax, variable naming rules, type inference with var and final, and differences between assignment and declaration. This lesson builds a foundation for managing state and data in Dart applications.

Variables are used for storing information that a computer program can access and manipulate. When building a physical project like a wooden bookshelf, we need planks of wood, nails, and different tools. To keep materials organized, we group similar items together and store them in labeled boxes. This allows us to find the exact item we need precisely when we need it.

In programming, a variable acts as a labeled box used to store data. When we declare a variable, we give it a unique name and define the type of data it can hold. When we assign a value to that variable, we are placing data into our labeled box.

Anatomy of a variable

Every programming language has a general set of rules for how code must be structured. These rules are collectively known as the language's syntax. Initializing a variable requires specific syntax components:

  • Type or keyword: Defines what kind of data the variable holds (like int for integer) or how it is inferred (like var).

  • Name: A unique identifier for the variable.

  • Assignment operator: The equals sign (=) used to assign a value.

  • Initial value: The starting data placed inside the variable.

<DataType> <Name> <AssignmentOperator> <InitialValue>;
...