When building applications with Dart—whether for mobile, web, or desktop using Flutter—understanding how to store and manage data is your very first stepping stone. Data in any programming language is handled through variables.

In this guide, we will cover everything you need to know about Dart variables, how to declare them using different keywords, and real-world code examples to solidify your understanding.

1. What is a Variable in Dart?

A variable is simply a named container in memory used to store values. You can think of a variable as a labeled box: the label is the variable name, and what you put inside the box is the value.

Dart is a soundly typed language. This means Dart ensures that the value assigned to a variable matches its declared type, preventing unexpected bugs while your app is running. However, Dart also features strong type inference, meaning it can often figure out the type of data automatically without you explicitly writing it out.

2. Rules for Naming Variables in Dart

Before declaring variables, keep these fundamental naming rules in mind:

  • Variable names are case-sensitive (age and Age are different variables).

  • Names must start with a letter (a-z, A-Z) or an underscore (_).

  • Names cannot start with numbers or contain special characters like @, #, $, or spaces.

  • Dart follows the lowerCamelCase naming convention (e.g., userAge, totalItemCount).

  • Reserved keywords (like class, void, import) cannot be used as variable names.

3. Standard Dart Data Types

Dart comes with several built-in types that cover the vast majority of use cases:

  • int: Integer values (e.g., 10, -5, 0).

  • double: Floating-point / decimal numbers (e.g., 3.14, 99.99).

  • num: The parent type of both int and double.

  • String: A sequence of UTF-16 text characters (e.g., "Hello Dart").

  • bool: Boolean values representing truth: true or false.

  • List: An ordered group of objects (arrays).

  • Map: A dictionary-like collection of key-value pairs.

  • Set: An unordered collection of unique items.

4. How to Declare Variables in Dart

Dart gives you multiple ways to declare variables depending on how you intend to use them.

A. Explicit Type Declaration

You can explicitly state the variable's type before its name. This makes your code highly readable and self-documenting.

Dart
String line = "Welcome to Dart!";
int score = 100;
double price = 19.99;
bool isLoggedIn = true;

B. Type Inference (var)

If you use the var keyword, Dart automatically infers the data type based on the initial value assigned to it. Once inferred, the type is fixed and cannot be changed later.

Dart
var username = "Alex"; // Dart infers username as String
var totalScore = 450;  // Dart infers totalScore as int

// username = 100; // ERROR: A value of type 'int' cannot be assigned to a variable of type 'String'.

C. Dynamic Types (dynamic)

If you need a variable that can hold values of any type and change its type at runtime, declare it with dynamic. (Note: Use dynamic sparingly, as it bypasses type-checking compile-time safety).

Dart
dynamic data = "John";
print(data); // Outputs: John

data = 42;    // Perfectly valid with dynamic
print(data); // Outputs: 42

D. Immutability (final vs const)

When a variable's value should never change after assignment, Dart provides two keywords: final and const.

  • final: A variable that can only be set once. Its value can be calculated at runtime (e.g., fetching current system time or API data).

  • const: A compile-time constant. The value must be known before the program runs.

Dart
// final: Set once at runtime
final DateTime currentDateTime = DateTime.now(); 

// const: Must be hardcoded or known at compile-time
const double pi = 3.14159;
const int maxLoginAttempts = 3;

5. Comprehensive Dart Code Example

Here is a complete, runnable Dart program demonstrating variable declarations, static typing, inference, and immutability:

Dart
void main() {
  // 1. Explicit Type Declarations
  String developerName = "Sarah Jenkins";
  int experienceYears = 5;
  double hourlyRate = 45.50;
  bool isAvailable = true;

  // 2. Type Inference using 'var'
  var currentProject = "Flutter E-Commerce App";

  // 3. Immutability with 'final' and 'const'
  final DateTime projectStartDate = DateTime.now();
  const String companyName = "TechSolutions Inc.";

  // 4. Collections
  List<String> requiredSkills = ["Dart", "Flutter", "REST APIs", "Git"];
  Map<String, String> contactInfo = {
    "email": "sarah@example.com",
    "github": "@sarah_dev"
  };

  // Printing the variables
  print("=== DEVELOPER PROFILE ===");
  print("Name: $developerName");
  print("Company: $companyName");
  print("Years of Experience: $experienceYears");
  print("Current Project: $currentProject");
  print("Project Start Date: $projectStartDate");
  print("Hourly Rate: \$$hourlyRate");
  print("Available for Hire: $isAvailable");
  print("Primary Skill: ${requiredSkills[0]}");
  print("Email: ${contactInfo['email']}");
}

Output:

Plaintext
=== DEVELOPER PROFILE ===
Name: Shahid Ali
Company: TechSolutions Inc.
Years of Experience: 5
Current Project: Flutter E-Commerce App
Project Start Date: 2026-11-08 00:09:00.000 (example timestamp)
Hourly Rate: $45.50
Available for Hire: true
Primary Skill: Dart
Email: shahid@example.com

6. Summary Cheat Sheet

KeywordDescriptionCan Change Value?Type Inferred?
String, int, etc.Explicitly typed variableYesNo
varImplicitly typed variableYes (same type only)Yes
dynamicAny type allowed at runtimeYes (any type)No
finalSingle-assignment runtime variableNoYes (optional explicit)
constFixed compile-time constantNoYes (optional explicit)

Wrapping Up

Understanding Dart variables and data types sets a solid foundation for writing clean, bug-free, and maintainable applications. As a best practice:

  1. Prefer explicit types or var for clear, readable code.

  2. Use final by default for variables that don't need to change.

  3. Reserve const for hardcoded constants.

  4. Avoid dynamic unless absolutely necessary.

Got questions about Dart variables or setting up your first environment? Leave a comment below!