Dart provides different types of variables based on their scope and lifecycle, including local variables, instance variables, and global variables.
Local Variables: These are declared inside a function or method and can only be accessed within that function or method.
Instance Variables: These are declared inside a class and can be accessed by creating an instance of the class.
Class Variables (Static Variables): These are declared inside a class with the static keyword and are shared by all instances of that class.
Global Variables: These are declared outside any class or function and are accessible globally within the Dart program.
Variables in Dart can be declared using:
var: The type is inferred by Dart.
dynamic: The variable can change its type at runtime.
Specific type: You can explicitly define the type (e.g., int, String, etc.).
Local Scope: A variable declared inside a function or block of code.
Global Scope: A variable declared outside any function or class (accessible throughout the program).
Instance Scope: A variable declared inside a class, but outside any methods.
Static Scope: A variable declared as static inside a class, shared among all instances.
dart
Copy code
void main() {
var localVar = 'I am a local variable'; // Local variable
print(localVar); // Accessible within main() function
// This will give an error if uncommented, as localVar is not accessible outside of main()
// print(localVar); // Error: The getter 'localVar' isn't defined for the class 'Main'.
}
dart
Copy code
class Person {
String name; // Instance variable
Person(this.name); // Constructor to initialize the instance variable
void greet() {
print('Hello, my name is $name');
}
}
void main() {
var person = Person('Alice'); // Creating an instance of the class
person.greet(); // Accessing the instance variable through the method
}
dart
Copy code
class Counter {
static int count = 0; // Static variable shared among all instances
Counter() {
count++; // Increment the static variable each time an instance is created
}
void displayCount() {
print('Count: $count');
}
}
void main() {
var counter1 = Counter(); // count becomes 1
var counter2 = Counter(); // count becomes 2
var counter3 = Counter(); // count becomes 3
counter1.displayCount(); // Count: 3
counter2.displayCount(); // Count: 3
counter3.displayCount(); // Count: 3
}
dart
Copy code
int globalVar = 100; // Global variable
void display() {
print('Global variable: $globalVar');
}
void main() {
display(); // Accessing global variable inside main()
globalVar = 200; // Modifying global variable
display(); // Accessing the updated global variable
}
dart
Copy code
void main() {
dynamic variable = 'Hello, Dart!'; // Initially a String
print(variable);
variable = 123; // Now it's an integer
print(variable);
variable = true; // Now it's a boolean
print(variable);
}
dart
Copy code
void displayMessage() {
var message = 'Hello from function!';
print(message); // Variable 'message' is accessible here
}
void main() {
displayMessage();
// This will give an error if uncommented because 'message' is not in scope
// print(message); // Error: The getter 'message' isn't defined for the class 'Main'.
}
dart
Copy code
int count = 10; // Global variable
void main() {
int count = 5; // Local variable (shadows the global one)
print('Local count: $count'); // Will print the local count (5)
print('Global count: ${globals()}'); // Accessing the global count
}
int globals() {
return count; // Accessing global count
}
dart
Copy code
void main() {
const double pi = 3.14; // Constant variable
print('Pi value: $pi');
// Uncommenting the following line will give an error since pi is a constant
// pi = 3.14159; // Error: Constant variables can't be assigned a value.
}
dart
Copy code
void main() {
final String name = 'Dart'; // Final variable
print('Name: $name');
// Uncommenting the following line will give an error since name is final
// name = 'Flutter'; // Error: A final variable can only be set once.
}
dart
Copy code
void main() {
for (int i = 0; i < 5; i++) { // Local variable i
print('Value of i: $i');
}
// Uncommenting the following line will give an error since i is not accessible outside the loop
// print(i); // Error: The getter 'i' isn't defined for the class 'Main'.
}
Local Variables: Declared inside functions and accessible only within those functions or blocks.
Instance Variables: Declared inside a class and accessible through class instances.
Static Variables: Declared with static inside a class and shared across all instances of the class.
Global Variables: Declared outside any class or function, accessible anywhere in the program.
Dynamic Variables: Declared with dynamic keyword, allowing the type to change at runtime.
Constant Variables: Declared using const, their value is fixed at compile-time.
Final Variables: Declared using final, their value can be set once and remains unchanged thereafter.
These examples cover local scope, global scope, instance scope, static scope, and more.
dart
Copy code
void main() {
var localVar = 'I am a local variable'; // Local variable inside the main function
print(localVar); // Accessible inside main function
// Uncommenting the line below will cause an error because localVar is out of scope.
// print(localVar); // Error: The getter 'localVar' isn't defined for the class 'Main'.
}
dart
Copy code
void displayMessage() {
var message = 'Hello from displayMessage() function';
print(message); // Accessible inside displayMessage
}
void main() {
displayMessage();
// Uncommenting the line below will cause an error because message is out of scope.
// print(message); // Error: The getter 'message' isn't defined for the class 'Main'.
}
dart
Copy code
int globalVar = 100; // Global variable
void display() {
print('Global variable value: $globalVar');
}
void main() {
display(); // Accessing the global variable inside main function
globalVar = 200; // Modifying the global variable
display(); // Accessing the updated global variable
}
dart
Copy code
class Person {
String name; // Instance variable
Person(this.name); // Constructor to initialize instance variable
void greet() {
print('Hello, my name is $name');
}
}
void main() {
var person1 = Person('Alice');
var person2 = Person('Bob');
person1.greet(); // Hello, my name is Alice
person2.greet(); // Hello, my name is Bob
}
dart
Copy code
class Counter {
static int count = 0; // Static variable shared among all instances
Counter() {
count++; // Increment static variable on each instance creation
}
void displayCount() {
print('Count: $count');
}
}
void main() {
var counter1 = Counter(); // count becomes 1
var counter2 = Counter(); // count becomes 2
var counter3 = Counter(); // count becomes 3
counter1.displayCount(); // Count: 3
counter2.displayCount(); // Count: 3
counter3.displayCount(); // Count: 3
}
dart
Copy code
int count = 10; // Global variable
void main() {
int count = 5; // Local variable with same name (shadows global variable)
print('Local count: $count'); // Prints the local count (5)
print('Global count: ${globals()}'); // Accessing global count through function
}
int globals() {
return count; // Accesses the global count variable
}
dart
Copy code
void main() {
final String name = 'Dart Programming'; // Final variable, cannot be changed
print('Name: $name');
// Uncommenting the following line will cause an error since final variables cannot be reassigned.
// name = 'Flutter'; // Error: A final variable can only be set once.
}
dart
Copy code
void main() {
const double pi = 3.14; // Constant variable, its value is set at compile time
print('Pi value: $pi');
// Uncommenting the following line will cause an error since constants cannot be reassigned.
// pi = 3.14159; // Error: Constant variables can't be assigned a value.
}
dart
Copy code
void main() {
for (int i = 0; i < 3; i++) { // Local variable i inside the loop
print('Value of i: $i');
}
// Uncommenting the following line will cause an error because i is out of scope.
// print(i); // Error: The getter 'i' isn't defined for the class 'Main'.
}
dart
Copy code
int num = 50; // Global variable
void main() {
int num = 10; // Local variable with the same name as global
print('Local num: $num'); // Prints the local num (10)
// Accessing the global variable by using a function
print('Global num: ${getGlobalNum()}'); // Prints the global num (50)
}
int getGlobalNum() {
return num; // Accesses the global num variable
}
Local Variables: Variables declared inside a function or block. Accessible only within that function or block.
Global Variables: Variables declared outside any functions or classes. Accessible anywhere in the program.
Instance Variables: Variables declared within a class but outside any methods. These are accessible through instances of the class.
Static Variables: Variables declared as static inside a class. Shared across all instances of the class.
Final Variables: Variables that can be assigned once and their value cannot be changed after that.
Constant Variables: Variables whose value is determined at compile time and cannot be modified.
Variable Shadowing: When a local variable has the same name as a global variable, the local one shadows the global one within its scope.
Loops and Scope: Loop variables are scoped to the loop block, meaning they are not accessible outside the loop.