Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart final and const


Share

Dart final

Sometimes a value should never change after you set it, like a birth year or a user ID.

For this, use the final keyword instead of var:

Example

void main() {
  final birthYear = 1995;
  print(birthYear);
}
Try it Yourself »

Result:

1995

A final variable can be set only once.

If you try to give it a new value, the program does not run:

Example

void main() {
  final birthYear = 1995;
  birthYear = 2000;
  print(birthYear);
}
Try it Yourself »

Result:

main.dart:3:3: Error: Can't assign to the final variable 'birthYear'.
  birthYear = 2000;
  ^^^^^^^^^

You can also write the type after final:

Example

void main() {
  final String country = 'Norway';
  final int population = 5500000;
  print(country);
  print(population);
}
Try it Yourself »

Result:

Norway
5500000

Dart const

The const keyword also creates a variable that cannot change:

Example

void main() {
  const pi = 3.14159;
  const daysInWeek = 7;
  print(pi);
  print(daysInWeek);
}
Try it Yourself »

Result:

3.14159
7

The difference is when the value is known.

A const value must be a compile-time constant.

This means that Dart must know the value before the program starts, just by reading the code.

Numbers, strings, true, false, and calculations with other constants are compile-time constants:

Example

void main() {
  const minutesPerHour = 60;
  const hoursPerDay = 24;
  const minutesPerDay = minutesPerHour * hoursPerDay;
  print(minutesPerDay);
}
Try it Yourself »

Result:

1440


final vs const

A final value can be found while the program runs.

For example, the current time is only known when the program runs, so it can be final:

Example

void main() {
  final now = DateTime.now();
  print(now.year > 2000);
}
Try it Yourself »

Result:

true

The example prints true, because the current year is greater than 2000.

The current time cannot be const, because Dart cannot know it before the program starts:

Example

void main() {
  const now = DateTime.now();
  print(now);
}
Try it Yourself »

Result:

main.dart:2:24: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
  const now = DateTime.now();
                       ^^^

A value from another variable is not a constant either, even if the variable does not change:

Example

void main() {
  var price = 10;
  const total = price * 2;
  print(total);
}
Try it Yourself »

Result:

main.dart:3:17: Error: Not a constant expression.
  const total = price * 2;
                ^^^^^
finalconst
Can be changed after it is setNoNo
Value can come from the running programYesNo
Value must be known before the program startsNoYes
Examplefinal now = DateTime.now();const pi = 3.14159;

const Lists

A list stores many values in one variable, inside square brackets [].

You will learn about lists in the Lists chapter.

A final list cannot be replaced with another list, but you can still change the items inside it:

Example

void main() {
  final fruits = ['Apple', 'Banana'];
  fruits.add('Cherry');
  print(fruits);
}
Try it Yourself »

Result:

[Apple, Banana, Cherry]

A const list is fixed, so you cannot add, remove or change items.

If you try, the program stops with an error when it reaches that line:

Example

void main() {
  const fruits = ['Apple', 'Banana'];
  print(fruits);
  fruits.add('Cherry');
}
Try it Yourself »

Result:

[Apple, Banana]
Unhandled exception:
Unsupported operation: Cannot add to an unmodifiable list

Tip: Use final for values that are set once and never changed.

Use const for fixed values that you can write directly in the code, like pi or a list of weekdays.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.