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