Variables

Store references.

Everything you can place in a variable is an object, and every object is an instance of a class.

Creating a variable and initializing it

  • If an object isn’t restricted to a single type, specify the Object or dynamic type.
  • There is an option to explicitly declare the type that would be inferred.
  • Variable may be created and initialized using var keyword.

Default value

Uninitialized variables have an initial value of null.

Even variables with numeric types are initially null, because numbers — like everything else in Dart — are objects.

final

final name = 'Bob';
  • A final variable can be set only once.
  • A final top-level or class variable is initialized the first time it’s used.
  • You can’t change the value of a final variable.

const

const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere
  • A const variable is a compile-time constant.
  • Const variables are implicitly final.
  • If the const variable is at the class level, mark it static const.
  • The const keyword isn’t just for declaring constant variables. You can also use it to create constant values, as well as to declare constructors that create constant values. Any variable can have a constant value.

Variables — Structure map

Clickable & Draggable!

Variables — Related pages: