You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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. A final variable 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 starts
    

    final is 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 a const object, which we'll get to).

  • const: This is a compile-time constant. Not only can you never reassign a const variable, but the value it holds must be known before your code even runs. Additionally, any object marked const is 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 time
    

    Use const for static values that never change, like configuration constants, mathematical values, or immutable data structures that don't depend on runtime state.

Are 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 constantSet is final, so you can't reassign it to a different set later.
  • The set { 'fluorine' } is a const (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 const on the right):
    final constantSet = {'fluorine'}; // Now it's a mutable HashSet
    constantSet.add('neon'); // This works perfectly fine!
    
    The final keyword only restricts reassignment of the variable itself—not mutability of the object it points to.

2. const constantSet = { 'fluorine' };

  • Here, constantSet is a const variable, which means it's implicitly final (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 const on the right side is optional here—since we're initializing a const variable, the compiler infers that the set should be const too. So const constantSet = { 'fluorine' }; is shorthand for const 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 10:13:11