Introduction
In Dart, constants are values that cannot be changed after they’re set. Using constants makes your code safer, more efficient, and easier to understand. Dart provides two keywords for creating constants:const and final.
Why Use Constants?
Safety
Prevent accidental modification of values that shouldn’t change
Performance
Compile-time constants are optimized by the compiler
Clarity
Make your intent clear - this value is not meant to change
Debugging
Easier to track down errors when values are immutable
const - Compile-Time Constants
Theconst keyword creates compile-time constants. These values must be known at compile time and are deeply immutable.
Naming Convention: Constants are traditionally written in UPPERCASE letters to make them easily identifiable in code.
Const Characteristics
- Must be assigned a value at compile time
- The value must be a compile-time constant expression
- Cannot be reassigned
- More memory efficient (values are canonicalized)
What Can Be const?
✅ Valid const values:- Numbers:
const MAX_SIZE = 100; - Strings:
const APP_NAME = 'MyApp'; - Booleans:
const IS_DEBUG = false; - Other const values:
const DOUBLE_MAX = MAX_SIZE * 2;
- Function calls (except const constructors)
- Values computed at runtime
- User input
- Current date/time
final - Runtime Constants
Thefinal keyword creates runtime constants. The value is set once when first accessed and cannot be changed afterward, but it doesn’t need to be known at compile time.
Final Characteristics
- Can be set at runtime
- Value can be the result of a function or computation
- Cannot be reassigned after initialization
- Each object can have its own final value
const vs final: Key Differences
When to Use const
When to Use const
Use
const when:- The value is known at compile time
- You want maximum performance optimization
- The value is truly universal and unchanging (like PI, mathematical constants)
- You’re defining configuration values that never change
When to Use final
When to Use final
Use
final when:- The value is determined at runtime
- The value comes from a function call or calculation
- Each instance needs its own immutable value
- You want to prevent reassignment but the initial value isn’t known until runtime
Attempting to Modify Constants
String Interpolation with Constants
You can embed constant values directly in strings using the$ symbol:
Practical Examples
Mathematical Constants
Configuration Values
Runtime Values
Best Practices
Do:
- Use UPPERCASE for
constvalues to make them easily identifiable - Use camelCase for
finalvalues (they behave like regular variables) - Choose
constoverfinalwhen possible for better performance - Use descriptive names that indicate the constant’s purpose
- Group related constants together
Complete Example
Key Takeaways
- Use
constfor compile-time constants (values known before runtime) - Use
finalfor runtime constants (values set once at runtime) - Constants cannot be reassigned after their initial value is set
- Use UPPERCASE for
const, camelCase forfinal - Constants improve code safety, performance, and readability
- String interpolation works with both
constandfinalvalues