Variables in Dart

Variables are used to store data in memory so it can be accessed and manipulated during program execution. In Dart, variables play a critical role because application state, user input, API data, and UI updates are all managed through variables.

Dart is a statically typed language with type inference, which means every variable has a type, but Dart can often infer it automatically.


Why Variables Matter in Flutter

In Flutter:

  • Widget data is stored in variables

  • UI updates depend on variable changes

  • App state is managed through variables

If you do not understand variables well, Flutter development becomes confusing very quickly.


Declaring Variables with Explicit Types

You can declare variables by explicitly specifying their data type.

void main() {
  int age = 22;
  double height = 5.9;
  String name = 'Rabin';
  bool isStudent = true;

  print(age);
  print(height);
  print(name);
  print(isStudent);
}

When to Use Explicit Types

  • When writing production code

  • When working in teams

  • When you want strict type safety

  • When teaching or documenting code


Using var (Type Inference)

The var keyword allows Dart to automatically infer the variable type from the assigned value.

void main() {
  var city = 'Kathmandu';
  var year = 2025;

  // city = 100; // ❌ Error: city is a String

  print(city);
  print(year);
}

Important points:

  • var does not mean dynamic

  • Once assigned, the type cannot change


Using dynamic

The dynamic keyword disables type checking at compile time. The variable can hold any type of value.

void main() {
  dynamic value = 'Hello';
  print(value);

  value = 100;
  print(value);

  value = true;
  print(value);
}

Warning

  • Avoid dynamic in Flutter unless absolutely necessary

  • It can cause runtime errors and bugs


final Variables (Single Assignment)

A final variable can be assigned only once. Its value cannot be changed after initialization.

void main() {
  final String country = 'Nepal';
  final int birthYear = 2002;

  // country = 'India'; // ❌ Error

  print(country);
  print(birthYear);
}

Common Use Cases

  • API responses

  • Configuration values

  • Widget properties


const Variables (Compile-Time Constants)

const variables are compile-time constants, meaning their value must be known before the program runs.

void main() {
  const double pi = 3.14159;
  const int maxUsers = 100;

  print(pi);
  print(maxUsers);
}

Difference Between final and const

final

const

Runtime constant

Compile-time constant

Value set once

Value fixed at compile time

More flexible

More strict


Nullable and Non-Nullable Variables (Null Safety)

Dart enforces null safety, meaning variables cannot hold null unless explicitly allowed.

Non-nullable Variable

void main() {
  String name = 'Rabin';
  // name = null; // ❌ Error
}

Nullable Variable

void main() {
  String? nickname;
  nickname = null;
  nickname = 'RB';

  print(nickname);
}

Late Variables

The late keyword tells Dart that a variable will be initialized before it is used, but not immediately.

late String token;

void main() {
  token = 'abc123';
  print(token);
}

Common in Flutter:

  • API data

  • Dependency injection

  • Variables initialized in initState


Common Beginner Mistakes

  1. Using dynamic unnecessarily

  2. Forgetting null safety (String vs String?)

  3. Confusing final and const

  4. Reassigning final variables


Summary

In this lesson, you learned:

  • What variables are

  • How to declare variables

  • Difference between var, explicit types, and dynamic

  • How final, const, late, and null safety work

A strong understanding of variables is essential for mastering Flutter state management.

Updated on