Dart 中 static 变量和 const 变量的区别

Dan*_*ico 9 compilation constants dart flutter

检查这两个例子:

static const课堂内:

class SessionStorage {
  static const String _keySessionExist = 'storage.key';
}
Run Code Online (Sandbox Code Playgroud)

只是const课堂之外:

const String _keySessionExist = 'storage.key';

class SessionStorage {

}
Run Code Online (Sandbox Code Playgroud)
  • static const在类内部使用变量或在类外部将其声明为 const 之间有什么区别或含义吗Dart
  • 也许编译后的代码发生了变化?
  • 哪一个性能更好?
  • 如果变量是文件私有的,我们应该遵循哪一个?

小智 21

The declaration for cons must be using const. You have to declare it as static const rather than just const.

static, final, and const mean entirely distinct things in Dart:

static means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else. static modifies members.

final means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables.

const has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.

Const objects have a couple of interesting properties and restrictions: They must be created from data that can be calculated at compile time. A const object does not have access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not. They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively. They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated. In other words:

getConst() => const [1, 2]; 

    main() { 
      var a = getConst(); 
      var b = getConst(); 
      print(a === b); // true 
    } 
Run Code Online (Sandbox Code Playgroud)

I think Dart does a pretty good job of keeping the semantics and the keywords nicely clear and distinct. (There was a time where const was used both for const and final. It was confusing.) The only downside is that when you want to indicate a member that is single-assignment and on the class itself, you have to use both keywords: static final.

Also:

I suggest you to have a look at this question

What is the difference between the "const" and "final" keywords in Dart?