Variables in Dart: A Beginner's Guide

If you're just stepping into the world of Dart, understanding the basics of variables is your key to unlocking the language's potential. Let's embark on a beginner-friendly journey into Dart variables.

1. Variable Declaration:

In Dart, you can declare a variable using the var keyword for automatic type inference or explicitly specify the type.

 

2. Data Types:

Dart supports various data types for variables, including:

  • Numbers: int (integers), double (floating-point numbers).
  • Strings: String (sequences of characters).
  • Booleans: bool (true or false values).
  • Lists: Ordered collections of values.
  • Maps: Unordered collections of key-value pairs.

 

3. Constants:

Dart allows the declaration of constants using the const keyword. Constants are values that remain unchanged throughout the program's execution, providing stability and preventing accidental modifications.

 

4. Dynamic Typing:

Dart provides the dynamic type for scenarios where a variable's type needs to be determined dynamically during runtime.

 

5. Scope and Lifetime of Variables

Variables in Dart have scope, which defines where in the code they can be accessed. Local variables are confined to a specific block of code, while global variables have broader accessibility. The lifetime of a variable is tied to its scope, ensuring efficient memory management.

 

6. Updating Variables:

Variables in Dart can be updated by assigning new values to them.

 

Example:

// This is a global Scope
String globalScope = 'Global';

void main() {
  
  // String variable
  String hello = 'Hello, World!';

  // Numeric variables
  int age = 25;
  double width = 120.5;

  // Boolean variable
  bool isOkay = true;

  // List variable
  List<String> countryLanguages = ['English', 'Hindi', 'Latin'];

  // Map variable
  Map<String, dynamic> student = {
    'name': 'Alex Wrong',
    'age': 21,
    'isStudent': true,
  };

  // Constant variable
  const int monthsInYear = 12;

  // Dynamic typing
  dynamic dynamicVariable = 'I miss you always';

  // This is a local Scope
  int localScope = 10;

  // Display original values
  print(hello);
  print('Age : $age');
  print('Width : $width');
  print('Are you okay? $isOkay');
  print('Programming languages: $countryLanguages');
  print('Student : $student');
  print('Total months in a year : $monthsInYear');
  print('Are you miss me? $dynamicVariable');
  print(globalScope);
  print(localScope);

  // Updating variables
  age = 23;
  width = 140.70;
  isOkay = false;
  countryLanguages.add('Tagalog');
  student['isStudent'] = false;

  // Display updated values
  print('\nAfter Updates!');
  print('Updated Age : $age');
  print('Updated Width : $width');
  print('Are you okay? $isOkay');
  print('Updated Country languages : $countryLanguages');
  print('Updated Student : $student');
}

In this example:

  • We declare variables of different types: String, int, double, bool, List, and Map.
  • The const keyword is used for a constant variable (daysInWeek).
  • The dynamic type allows the variable dynamicVariable to hold any type of value.
  • We print the values of these variables using the print function.
  • Variables are then updated to demonstrate how values can be changed during program execution.

Variables serve as the cornerstone of Dart programming, empowering developers to efficiently handle and mold data. Proficiency in declaring, initializing, and utilizing variables is paramount, laying the groundwork for the creation of impactful and eloquent Dart applications.