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

前端 未结 11 1196
温柔的废话
温柔的废话 2020-11-27 10:52

What is the difference between const and final keyword in Dart?

相关标签:
11条回答
  • 2020-11-27 11:30

    Extending the answer by @Meyi

    • final variable can only be set once and it is initialized when accessed.(for example from code section below if you use the value of biggestNumberOndice only then the value will be initialized and memory will be assigned).
    • const is internally final in nature but the main difference is that its compile time constant which is initialized during compilation even if you don't use its value it will get initialized and will take space in memory.

    • Variable from classes can be final but not constant and if you want a constant at class level make it static const.

    Code:

    void main() {
    
        // final demonstration
        final biggestNumberOndice = '6';
        //  biggestNumberOndice = '8';     // Throws an error for reinitialization
    
        // const
        const smallestNumberOnDice = 1;
    
    }
    
    class TestClass {
    
        final biggestNumberOndice = '6';
    
        //const smallestNumberOnDice = 1;  //Throws an error
        //Error .  only static fields can be declared as constants.
    
        static const smallestNumberOnDice = 1;
    }
    
    0 讨论(0)
  • 2020-11-27 11:35

    There is a post on dart's website and it explains it pretty well.

    Final:

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

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


    So, what does this mean?

    Const:
    If the value you have is computed at runtime (new DateTime.now(), for example), you can not use a const for it. However, if the value is known at compile time (const a = 1;), then you should use const over final. There are 2 other large differences between const and final. Firstly, if you're using const, you have to declare it as static const rather than just const. Secondly, if you have a const collection, everything inside of that is in const. If you have a final collection, everything inside of that is not final.

    Final:
    final should be used over const if you don't know the value at compile time, and it will be calculated/grabbed at runtime. If you want an HTTP response that can't be changed, if you want to get something from a database, or if you want to read from a local file, use final. Anything that isn't known at compile time should be final over const.


    With all of that being said, both const and final cannot be reassigned, but fields in a final object, as long as they aren't const or final, can be reassigned (unlike const).

    0 讨论(0)
  • 2020-11-27 11:35

    When to use which keyword?

    A simple example for both: Use final: If you don’t know what it’s value will be at compile-time. For example, when you can need to get data from an API, this happens when running your code.

    Use const: If you are sure that a value isn’t going to be changed when running your code. For example, when you declare a sentence that always remains the same.

    https://itnext.io/difference-between-const-and-final-in-dart-78c129d0c573

    0 讨论(0)
  • 2020-11-27 11:35

    const is a Compile-time constant.

    final is a Run-time constant.

    0 讨论(0)
  • 2020-11-27 11:36

    final and const in dart are confusing to the level we think both of them are the same.

    Let's see their differences :

    P.S. I included image instead of text as I couldn't tabulate the info in Stackoverflow .md format easily.

    0 讨论(0)
提交回复
热议问题