Types of Dart Variables
Types of Dart Variables
The Dart programming language supports many variables based on their mutability and scope. Mutability refers to whether a variable’s value can be changed or modified after it has been initialized. The scope of a variable refers to the region of the code where that variable is visible and can be accessed.
Types of variables
In Dart, variables can be classified into the following types:
- Mutable variables
- Immutable variables
- Instance variables
- Class variables
- Local variables
Mutable variables
A mutable variable is one whose value can be changed after it is assigned. In Dart, the var keyword or using data type like int, double, String, etc., without using final or const creates a mutable variable.
Immutable variables
An immutable variable is one whose value cannot be changed after it is assigned. Once initialized, its value remains constant throughout the program’s lifetime. Dart provides two keywords for creating immutable variables
- final
- const
Instance variables
Instance variables are also known as non-static variables. These variables are declared without the static keyword. The instance variables are specific to an object. These variables can be accessed using the instance of that class.
Class variables
Class variables are also known as static member variables. These variables are declared using the static keyword. A class variable is declared in the class but outside a constructor, method, or block. All instances of the class share one copy of the class variable.
Local variables
Local variables are defined in the methods, constructors, or blocks. It is created when we create a method or constructor, and it has scope only inside them. We cannot use a local variable outside the method, constructor, or block.
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/