The flutter “const” Keyword demystified.

Kefeh Collins
Flutter Community
Published in
4 min readJul 7, 2021
Flutter const keyword demystified

The const keyword has been a subject of debate and discussion in the flutter community, from guys complaining in utter frustration about the new linting rules that force them to introduce const at several points in the app and also forces them to remove them where not needed. Some on the other hand are hailing the const keyword to say it’s the holy grail for performance improvement and optimization.

But what does the const keyword do? and does it matter? In this article, I will be sharing my knowledge and “explored” information that may help drive the point.

What is the const keyword and why should we care?

The const keyword is used when the value of the variable is known at compile-time and never changes. In other words, the compiler knows in advance what value is to be stored in that variable.

const int x = 1;//At compile time, the value of x is going to be 1 and will not change.

Flutter also automatically deduces the type of the variable when you just declare and initialize it as a const variable.

const name = "John Doe";

But why the fuss then? If I use the const keyword just to indicate that a value will be constant, why should I care? Or why should I not just go the normal way and do?

int x = 1;

Actually yes, you can just go your normal way and do as before, because really, the difference is not so evident here. Or really?

The actual use of the const keyword is to serve as an indicator that tells the compiler that, “for this variable, it will never change in the lifetime of this code, so create just one copy of it and where ever it is mentioned, just reference back to the one copy you already created and don’t create a new one.”

Before we continue further, it’s worth noting that const is used for compile-time constants, like Strings, numbers, doubles and even Classes. Yes, classes.

Going back to the importance of const, its importance can be very evident when we have classes and their constructors. Flutter deals with widgets and each widget is a class. Imagine creating a widget that does something specific and never changes like to just create a blue square of a particular size.

If you decide that in your app, you will have 100 blue squares with size 50,

You will normally create for each of the 100 objects an instance of the BlueSquare

BlueSquare(size:50);

Let’s think of it this way if Flutter was to create in-memory each of the 100 BlueSquare objects, that will be a waste of memory, if it could just create one and then whenever you try to create a new object that is the same size, it just looks up its cache and give you the “reference” to the object right?.

Perfect, the const keyword is therefore useful here to be that thing that informs flutter what to do when it encounters a constructor that will not change in other words a constant constructor.

A constant constructor is a constructor which initializes all its final fields, and it is often denoted with a const in front of the constructor.

If you don’t however add the const keyword in front of the constructor, then even if it satisfies the conditions to be const, it will not be considered as such by Flutter and you can not initialize it with a const. In our example above, the BlueSquare widget satisfies it, lets, therefore, enforce it to be constant

And then we create objects as

const BlueSquare(size: 50);

In essence, the const constructor creates canonical types that make it easy to evaluate equality. If you create.

Since they are really pointing to the same object. If you have a const constructor, is because you want to make sure that the fields in the class (normally) never change and that’s why you have to define all the fields with a final keyword, else you have an error.

This makes sense because if you have an object that will change, why would you want to tell the compiler that it is constant? (sounds contradictory).

The fact that it saves memory is not the only importance of const widgets. Because an object is marked as constant, flutter knows that in the case of a rebuild, it makes no sense to rebuild that object since “it should not have changed”. This is a great performance boost as once an object is built, in the lifetime of the app, it will remain the same, and that is just spectacular and improves even the hot restart functionality of flutter as it knows where and what to rebuild based on changes.

I would have concluded my article here, but there is no talking of const without mentioning the final keyword since many developers tend to use them interchangeably and most often when face with the choice of choosing always resort to final rather than const.

final int x = 50;
const int x = 50;

The const and final are in fact very similar as they both define values that should not change. But final is more like the less strict form of const in that, it holds values that should not change but those values can be unknown for a period of time even after compilation, but once it is known, it never changes.

final response =
await http.get(Uri.parse(‘https://jsonplaceholder.typicode.com/albums/1'));

Lastly, instance variables can only be final and not const and static variables is preferred to be const over final because const declarations are more hot-reload friendly and can saves memory since static variables are not meant to change why not tell the computer that by using the const “indicator”?.

I hope this exposition hopefully demystified the const keyword, its importance and its uses.

Thanks for reading and see you in the next one.

Follow Flutter Community on Twitter: https://twitter.com/FlutterComm

--

--

Kefeh Collins
Flutter Community

An Enthusiastic problem solver, uses code as a tool for problem solving.