Dart中final与const的差异及const不同写法的疑问
Awesome question—this is a super common point of confusion when getting started with Dart, so let's break it down clearly.
final vs const: Key Differences & Use Cases First, let's clarify what each keyword does and when to use one over the other:
final: Think of this as a runtime constant. Afinalvariable can only be assigned once, but that assignment can happen at runtime with a value that isn't known until your code runs. For example:final currentUser = fetchUserFromAPI(); // Value comes from a runtime network call final appLaunchTime = DateTime.now(); // Captures the exact time the app startsfinalis perfect for values that are fixed once your app is running, but can't be hardcoded during compilation. It guarantees the variable won't be reassigned later, but the object it points to might still be mutable (unless you initialize it with aconstobject, which we'll get to).const: This is a compile-time constant. Not only can you never reassign aconstvariable, but the value it holds must be known before your code even runs. Additionally, any object markedconstis deeply immutable—you can't change any part of it after creation. Examples:const gravity = 9.81; // A fixed physical constant const nobleGases = {'fluorine', 'neon', 'argon'}; // Immutable set, fixed at compile timeUse
constfor static values that never change, like configuration constants, mathematical values, or immutable data structures that don't depend on runtime state.
final x = const { ... } and const x = { ... } Equivalent? Short answer: No, they're not the same—even though both create variables that can't be reassigned. Let's use your example to explain:
1. final constantSet = const { 'fluorine' };
- The variable
constantSetisfinal, so you can't reassign it to a different set later. - The set
{ 'fluorine' }is aconst(immutable) object, so you can't add/remove items from it. - However, if you wanted to, you could modify this declaration to use a mutable set instead (by removing the
conston the right):
Thefinal constantSet = {'fluorine'}; // Now it's a mutable HashSet constantSet.add('neon'); // This works perfectly fine!finalkeyword only restricts reassignment of the variable itself—not mutability of the object it points to.
2. const constantSet = { 'fluorine' };
- Here,
constantSetis aconstvariable, which means it's implicitlyfinal(can't be reassigned) and the set it references is a compile-time immutable object. - You can never assign a non-const object to this variable, and you can't modify the set in any way:
const constantSet = {'fluorine'}; constantSet.add('neon'); // Throws an error: Cannot modify an unmodifiable set - Also, in Dart 2+, the
conston the right side is optional here—since we're initializing aconstvariable, the compiler infers that the set should beconsttoo. Soconst constantSet = { 'fluorine' };is shorthand forconst constantSet = const { 'fluorine' };.
What about const final vs final const?
If you see someone write const final x = { ... } or final const x = { ... }, these are equivalent to just const x = { ... }. Since const variables are already implicitly final, adding the final keyword doesn't change any behavior—it's just redundant syntax.
内容的提问来源于stack exchange,提问作者joshpetit

