On this page12 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. final: Set Once
  4. 2. const: Fixed Before the Program Runs
  5. 3. final vs const
  6. 4. Constant Lists
  7. 5. Which One to Use
  8. Code Examples
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 008 — Constants with final & const

Category3 — Variables & Constants
Lecture008 of 057
Estimated video length12 min
TargetDart 3.12
LevelBeginner

Some values must never change once set: a birth date, a tax rate, the number of days in a week. final and const lock them — and Dart stops anyone who tries to change them.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lectures 006–007: variables and data types.


Learning Objectives

  • Make a value unchangeable with final
  • Make a compile-time constant with const
  • Tell final and const apart
  • Know what a const list is
  • Choose between var, final and const

1. final: Set Once

dart
final name = 'Usman';
final String city = 'Multan';   // the type can be written too

// name = 'Ali';   // error: 'name' can only be set once

A final value can come from anywhere — even something only known while the program runs:

dart
final startedAt = DateTime.now();   // the current time, then locked

2. const: Fixed Before the Program Runs

dart
const daysInWeek = 7;
const pi = 3.14159;
const appName = 'Dart Basic';

// pi = 3;   // error: constant variables can't be assigned a value

A const value must be known when the code is compiled — written right there in the code, or calculated from other constants:

dart
const minutesPerDay = 24 * 60;    // fine: made only of constants
// const now = DateTime.now();    // error: the time isn't known until it runs

3. final vs const

finalconst
Can change after it's set?nono
Value known when?when the program runswhen the code is compiled
DateTime.now() allowed?yesno
Typical usevalues you calculate or load oncefixed numbers, names, limits

4. Constant Lists

dart
final scores = [90, 85];
scores.add(70);           // fine: the variable is locked, the list isn't

const days = ['Mon', 'Tue', 'Wed'];
// days.add('Thu');       // crashes: a const list can't be changed

final locks the variable — it always points to the same list, but that list can grow. const freezes the value too — the list itself can never change.

5. Which One to Use

  1. Fixed and known in advance (7, 'Dart Basic', 0.05)? → const.
  2. Set once, but calculated or loaded? → final.
  3. Needs to change later (a counter, a score)? → a normal variable (var or a type).

Most variables turn out to be final. Start with final, and change it only when you really need to reassign.


Code Examples

Example 1 — A Bill With Fixed Rates

dart
void main() {
  const taxRate = 0.05;
  const shopName = 'Corner Store';
  final itemName = 'Notebook';
  var quantity = 3;
  final unitPrice = 120.0;

  quantity = quantity + 1;   // fine: a normal variable
  // taxRate = 0.1;          // error: taxRate is const

  final subtotal = quantity * unitPrice;
  final total = subtotal + subtotal * taxRate;

  print(shopName);
  print('$quantity × $itemName');
  print('Subtotal: $subtotal');
  print('Total with tax: $total');
}

Output

code
Corner Store
4 × Notebook
Subtotal: 480.0
Total with tax: 504.0

Common Beginner Mistakes

1. const with a runtime value — const now = DateTime.now(); is an error. Use final.

2. Thinking final freezes a list — final list = [1]; list.add(2); works. Use const for a list that must never change.

3. Writing final var — it's final name = ... or final String name = ..., never both keywords.


Practice Questions

  1. Which keyword fits a value read from the clock once? Which fits 365?
  2. Is const total = 60 * 60; allowed?
  3. After final list = [1, 2];, does list.add(3); work?
  4. What happens if you try to change a const variable?

Practice Answers

  1. final; const.
  2. Yes — it is made only of constants.
  3. Yes — final locks the variable, not the list.
  4. The code doesn't compile.

Key Takeaways

  • final = set once, value may be calculated while running.
  • const = fixed when compiled; the value itself is frozen.
  • A final list can still change; a const list can't.
  • Default to final; use const for fixed values; plain variables only when they change.