Variables are used to store data in memory. Dart supports various types of variables, including integers, doubles, strings, booleans, and more. Here's a brief overview of how to declare and use variables in Dart.
int): Stores whole numbers.double): Stores floating-point numbers.String): Stores text.bool): Stores true or false.var): Can store values of any type, determined at runtime.const): Compile-time constant, value cannot be changed.final): Value can be assigned only once, can be set at runtime.void main() { // Declare and initialize variables
// Integer variable int age = 25; print(age); // Output: 25
// Double variable double height = 5.9; print(height); // Output: 5.9
// String variable String name = "Alice"; print(name); // Output: Alice
// Boolean variable bool isStudent = true; print(isStudent); // Output: true
// Dynamic variable (can hold any type of value) var dynamicVariable = 10; // Initially an integer print(dynamicVariable); // Output: 10
dynamicVariable = "Now I'm a string"; print(dynamicVariable); // Output: Now I'm a string
// Constant variable (cannot be changed once assigned) const int constantValue = 100; print(constantValue); // Output: 100
// Final variable (similar to const, but can be assigned at runtime) final currentTime = DateTime.now(); print(currentTime); // Output: Current date and time }