20

Can someone explain to me how/when/why to use const keyword, or it is just "a way to declare a constant variable"? If so, what's the difference between this :

int x = 5;

and

const int x = 5;

Could you guys please give me an example?

nbro
  • 15,395
  • 32
  • 113
  • 196
Otskimanot Sqilal
  • 2,332
  • 4
  • 21
  • 25
  • Possible duplicate of [what is the difference in between ''const'' and ''final'' keyword in Dart?](https://stackoverflow.com/q/50431055/608639) – jww Dec 27 '18 at 15:37

2 Answers2

23

const means compile time constant. The expression value must be known at compile time. const modifies "values".

From news.dartlang.org,

"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.

if you use

const x = 5 then variable x can be used in a cosnt collection like

const aConstCollection = const [x];

if you don't use const, and just use x = 5 then

const aConstCollection = const [x]; is illegal.

More examples from www.dartlang.org

class SomeClass {
  static final someConstant = 123;
  static final aConstList = const [someConstant]; //NOT allowed
}

class SomeClass {
  static const someConstant = 123; // OK
  static final startTime = new DateTime.now(); // OK too
  static const aConstList = const [someConstant]; // also OK
}
Senthil Kumar
  • 9,695
  • 8
  • 36
  • 45
  • why can't I use const without static? My const doesn't need static class scope so why am I forced to use declare it as static? – robbie Feb 11 '14 at 23:11
  • @robbie your constant also doesn't need to be repeated for each instance. Since it is constant, it will never change, so why waste space duplicating it? By forcing static declaration of constants, they are only put into memory once for the class, rather than each time an instance is created. I believe that this explanation applies to both C# and Dart. – Carl G Feb 23 '14 at 04:30
  • 1
    Const values in Dart are "canonicalized", so there is only ever one instance of them, and "static const" is therefore redundant: "const" would be sufficient. The last paragraph of [Seth Ladd's article on this](http://news.dartlang.org/2012/06/const-static-final-oh-my.html) in fact suggests that "const" is the preferred style and "static const" discouraged. – dharcourt Apr 01 '14 at 20:48
  • What's the difference between `final` and `const` then? – CopsOnRoad Jul 19 '18 at 12:25
  • @CopsOnRoad https://stackoverflow.com/questions/50431055/what-is-the-difference-in-between-const-and-final-keyword-in-dart/51448666#51448666 – Faisal Naseer Jul 20 '18 at 18:49
3

Here are some facts about const values:

  • The value must be known at compile time.

    const x = 5; // OK
    
  • Anything that is calculated at runtime can't be const.

    const x = 5.toDouble(); // Not OK
    
  • A const value means that it's deeply constant, that is, every one of its members is constant recursively.

    const x = [5.0, 5.0];          // OK
    const x = [5.0, 5.toDouble()]; // Not OK
    
  • You can create const constructors. That means that it is possible to create const values from the class.

    class MyConstClass {
      final int x;
      const MyConstClass(this.x);
    }
    
    const myValue = MyConstClass(5); // OK
    
  • const values are canonical instances. That means that there is only a single instance no matter how many you declare.

    main() {
      const a = MyConstClass(5);
      const b = MyConstClass(5);
      print(a == b); // true
    }
    
    class MyConstClass {
      final int x;
      const MyConstClass(this.x);
    }
    
  • If you have a class member that is const, you must also mark it as static. static means it belongs to the class. Since there is only ever one instance of const values, it wouldn't make sense for it to not be static.

    class MyConstClass {
      static const x = 5;
    }
    

See also

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393