Dart Crash Course 3 - Final vs Const

Опубликовано: 02 Август 2026
на канале: Coding Chess
14
1

In Dart, both `final` and `const` are used to declare variables whose values cannot be changed once assigned. However, there are subtle differences between the two:

1. Final :
The `final` keyword is used to declare variables whose values can be set once and then cannot be changed.
The value of a `final` variable can be assigned either at compile-time or run-time.
The value of a `final` variable can be computed dynamically, allowing for more flexibility compared to `const`.
`final` variables are initialized when accessed, meaning they can have different values at runtime.

2. Const :
The `const` keyword is used to declare compile-time constants, i.e., values that are known and fixed at compile-time.
The value of a `const` variable must be known at compile-time and cannot be computed dynamically.
`const` variables are implicitly `final`, meaning they also cannot be reassigned after initialization.
`const` variables are evaluated and initialized at compile-time, resulting in improved performance and efficiency.

In summary, `final` is used for variables whose values can be determined at runtime but remain constant once assigned, while `const` is used for variables whose values are known at compile-time and remain constant throughout the program's execution.