Simplifying Flutter Const Keyword
People always love to add their touch to something that's what makes it unique and that's where the miscommunication starts. And this just does not happen in businesses or organizations, it happens in our home too and as a result it creates a mess. Same like these there are always wars between business, internal teams and sometimes even between developers where one side say no to using const keyword because it frustrates developers about so many new linting rules whereas other side say it's their heart desire and best for performance optimization.
Now you’ll be thinking what the hell does this const keyword do? Why is it so important?
In this article, we’ll be answering your question.
What Is The Const Keyword and Why Should We Care About It?
The const keyword is the variable value which never changes at compile time. In short, the compiler predicts what value is to be stored in the variable itself.
Flutter spontaneously gathers variable type information when you confirm and process the same as a const variable.
What’s so big about const keywords ? If we ‘ve to constant a variable we can do that in a normal way? Did these questions come to your mind?
Not able to find the answer yet ?
Don’t worry, after reading the complete article you’ll definitely have. Then why wait? Scroll above and read the article.
const int x = 1;
//At compile time, the value of x is going to be 1 and will not change.
const name = "John Doe";
int x = 1;
Well, the const keyword is used as an indicator that tells the compiler about the variable that it will never change in the whole life of this code. In simplest words, only for you, you can just create one copy of that const keyword variable and wherever you want to use you can just reference back to the copy and you don’t have to create a fresh one.
Before we proceed further, it's important to take note that const is used for compile time constants, like numbers, strings or even doubles.
Let's get back to the importance of const keyword, its importance is proven especially when we have classes and their constructors. As we have discussed in our earlier articles also that Flutter involves widget or I should say multiple widgets and each widget is called class. Assume creating a widget for something specific which never changes like create a blue square in particular size with green color.
If you choose the same you’ll be having 100 green squares with size 50. Elsewise, you’ll be creating normally each of the 100 green squares.
Okay, now think that if you’ve to create these 100 squares through flutter in memory how much of memory it will consume? Whereas if you could just create one and can use it whenever or wherever you want to, by just giving reference back to the object, don’t you think how convenient and time saving it would be.
Did you get that? How const keyword is useful when the object infuses flutter what to do when it bumps into a constant constructor, which cannot be changed.
A constructor which divides all its final fields is called constant constructor and it is frequently denoted with a const in front of the constructor.
Make sure that you add const keyword in front of the constructor because if you didn’t, it doesn’t matter whether it satisfies the const conditions or not, it will not be considered by the flutter and you can not carve up with a const. In our example above, the greensquare widget satisfies it, lets, therefore, enforce it to be constant.
After that we create object as,
When you’ll create , you’ll get to know that const constructor creates canonical types which makes evaluation easy and gives clear equality.
Since it indicates the same object. If you want to use const constructor that means , you don’t want to change the field in the class ever. That’s why you’ve to clearly define the final keyword otherwise you’ll have an error.
Somehow it makes sense, because if the object you have can change you definitely do not want, compiler to know about it.
Well, const widgets save a lot of memory is not the only importance it has but also it increases the performance and improves hot reload functionality. Because, when an object is denoted as constant , flutter knows clearly that there is no point to rebuild the same when it will not change.
As flutter knows clearly where, when and what to rebuild based on the recent changes.
Alright!! How can we end it like this ? without the final keywords as many developers use them reciprocally and frequently when it's a face choice between final and const, developers choose const.
The final and const are quite similar to each other , especially about defining values ,which should not change. But the norms of final are less strict than the const, but identifying those values isn’t easy but once you find it , it can’t be changed.
Last but not least, final includes only instance variables whereas the const includes static variables , because const has more hot-reload functionality than the final and it can save memory too. Static variables can’t be changed but we can make it by using const indicators.
Closing for today! We hope we were able to simplify the const keyword for you. Stay tuned for more articles!
final int x = 50;
const int x = 50;
final response =
Green Square" ;