On this page12 sections
Lecture 008 — Constants with final & const
| Category | 3 — Variables & Constants |
| Lecture | 008 of 057 |
| Estimated video length | 12 min |
| Target | Dart 3.12 |
| Level | Beginner |
Some values must never change once set: a birth date, a tax rate, the number of days in a week.
finalandconstlock 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
finalandconstapart - Know what a
constlist is - Choose between
var,finalandconst
1. final: Set Once
final name = 'Usman';
final String city = 'Multan'; // the type can be written too
// name = 'Ali'; // error: 'name' can only be set onceA final value can come from anywhere — even something only known while the program runs:
final startedAt = DateTime.now(); // the current time, then locked2. const: Fixed Before the Program Runs
const daysInWeek = 7;
const pi = 3.14159;
const appName = 'Dart Basic';
// pi = 3; // error: constant variables can't be assigned a valueA const value must be known when the code is compiled — written right there in the code,
or calculated from other constants:
const minutesPerDay = 24 * 60; // fine: made only of constants
// const now = DateTime.now(); // error: the time isn't known until it runs3. final vs const
final | const | |
|---|---|---|
| Can change after it's set? | no | no |
| Value known when? | when the program runs | when the code is compiled |
DateTime.now() allowed? | yes | no |
| Typical use | values you calculate or load once | fixed numbers, names, limits |
4. Constant Lists
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 changedfinal 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
- Fixed and known in advance (
7,'Dart Basic',0.05)? →const. - Set once, but calculated or loaded? →
final. - Needs to change later (a counter, a score)? → a normal variable (
varor 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
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
Corner Store
4 × Notebook
Subtotal: 480.0
Total with tax: 504.0Common 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
- Which keyword fits a value read from the clock once? Which fits
365? - Is
const total = 60 * 60;allowed? - After
final list = [1, 2];, doeslist.add(3);work? - What happens if you try to change a
constvariable?
Practice Answers
final;const.- Yes — it is made only of constants.
- Yes —
finallocks the variable, not the list. - 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
finallist can still change; aconstlist can't. - Default to
final; useconstfor fixed values; plain variables only when they change.