Difference between static and const variable in Dart

1,864

Solution 1

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 
    } 

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?

Solution 2

  • Is there any difference or implications between having a static const variable inside of a class or just having it declared as const outside of it in Dart?

The obvious difference is that the static version must be referenced with the class name. Other than the change in name resolution, the should be the same.

  • Maybe the compiled code changes?
  • Which one is more performant?

They're both compile-time constants. There shouldn't be any difference.

  • Which one should we follow if the variable is private to the file?

If you want something that's private to a Dart library (which usually means the file), then prefix it with _. It doesn't matter whether it's global or static.

Share:
1,864
Daniel Gomez Rico
Author by

Daniel Gomez Rico

Me

Updated on December 27, 2022

Comments

  • Daniel Gomez Rico
    Daniel Gomez Rico over 1 year

    Check these two examples:

    static const inside of class:

    class SessionStorage {
      static const String _keySessionExist = 'storage.key';
    }
    

    Just a const outside of the class:

    const String _keySessionExist = 'storage.key';
    
    class SessionStorage {
    
    }
    
    • Is there any difference or implications between having a static const variable inside of a class or just having it declared as const outside of it in Dart?
    • Maybe the compiled code changes?
    • Which one is more performant?
    • Which one should we follow if the variable is private to the file?
  • Daniel Gomez Rico
    Daniel Gomez Rico over 3 years
    You say "There shouldn't be any difference." but how can I be sure about that? I came from java, where the compiled files are still readable, so I can see how my code changed in the compiled artifacts, is there any way to do this in dart?
  • qbantek
    qbantek almost 2 years
    The above answer is almost entirely copied from a post by Bob Nystrom which was later re-posted by Seth Ladd. Bob Nystrom is a software engineer at Google working on the Dart programming language and Seth Ladd is the Product Manager for Flutter.