What is the purpose of assigning `const` value to a `final` variable in dart?

3,651

I personally think

final _biggerFont = const TextStyle(fontSize: 18.0);

looks like a mistake, and that that alone is reason enough to change it.

The member is only used inside the same class, so there is no reason not to make it static. Then it will not take up one extra memory location for each instance of the class, all pointing to the same value. (That's assuming the compiler doesn't recognize the field as always having the same value, and just inlines the value everywhere).

If the member is static, it might as well also be const, so I'd write it as:

static const _biggerFont = TextStyle(fontSize: 18.0);

This assumes that the code is what I found by searching for final _biggerFont = const.

If there is a subclass in the same library which overrides _biggerFont, then it does need to be an instance variable. It could still be a getter instead of a field, then. Whether that's an efficiency improvement depends on how the class is used, and how well a compiler optimizes a final field that always has the same value.

In any case, creating a private instance member which always has the same constant value looks like something that should just be a static constant to begin with, and code that looks like a mistake is confusing to read. I'd rewrite it (or document it) just for that reason - to avoid the reader being confused about why it is the way it is.

Share:
3,651
Daksh Gargas
Author by

Daksh Gargas

"That if statement had something else afterwards. (Pun intended)" I miss working on swift. Loves to play with 0s and 1s. Prefers using enums.

Updated on December 05, 2022

Comments

  • Daksh Gargas
    Daksh Gargas 6 minutes

    So I was doing the first example for Flutter, and under Step 4: Create an infinite scrolling ListView,

    I encountered this piece of code:

    class RandomWordsState extends State<RandomWords> {
      final _suggestions = <WordPair>[];
      final _biggerFont = const TextStyle(fontSize: 18.0);
      ...
    } 
    

    But I found the following line a little spooky.

    final _biggerFont = const TextStyle(fontSize: 18.0);
    

    My question is, what is the purpose of assigning a constant value to a final variable?

    I know that

    Compile-time constants are canonicalized,i.e. no matter how many times you write const MyObj(0, 0),you only create one object.

    This may sound useful, but you can simply create the const variable to hold the value and use that variable instead.

    Well, don't you think it's kinda redundant? I get it that the developers at Flutter wanted to create a compile-time constant object, but hey! you are assigning that value to a final variable. Which is somewhat the same thing.

    Any thoughts?

    UPDATE

    I googled some definitions, I found that

    const constructors cannot have a body and It's class must not have any non-final fields

    So is this the reason why we used the const keyword? Because if you'll look at the TextStyle class's design, you'll realize that they have done the exact same thing here.