Final vs. Constant Variables in Dart

SpaceBucket
4 min readOct 24, 2022

--

Before diving into the final vs constant variables in the dart, first, let’s try to understand the difference between the compile time and run time.

Let’s GooooooOOOO!!

So compile time is the instant at which the source code written by you (in any language) is converted into the executable code, while the run time is the instant at which the executable code has started running. For example, you are taking the current date and time:

const DateTime currentDateTime = DateTime.now();

so in this case the DateTime of that instant will be called when you run this line of code, it can be assigned to any value based upon current date and time.

However, if you take

const double height = 5.10;

in this case the compiler knows that its value won’t be changing later in the code. Therefore, it is converted to the executable code at this very moment. However, for currentDateTime it has to pick different value every time it is called during the compilation. I hope you now know the difference between the compile time and run time.

Compile time is the instant at which your code is converted to the executable however, the run time is the instant during which the program is running in real time and accessing the various run time commands and variables.

The historic fight between the const and finals! TANG TANG TANG! Fighttt!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

The beginners are always confused between the keywords const and final. In this blog, we will be learning about the difference between these two and will be understanding when to utilize the power of which one.

Final:

A final variable can only be assigned once. For a final, it is a must to have an initializer, and once initialized with a value it cannot be reassigned. For example, you can have only one date of birth, well you cannot be reborn that’s why your date of birth won’t be changing. In the same way, the final variables operate, once they are initialized, they cannot be initialized again.

final name = “SpaceBucket”; 
name = “some other name”; // error

Dart will never allow you to change the value of name. A final variable may rely on runtime execution of code to determine its state, but it must occur before initialization.

When should you use final over const?

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, or want to get something from a database, or to read from a local file, use final. Anything that isn't known at compile time should be final over const.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Constants:

Constants are compile-time constants. Use const for variables that you want to be compile-time constants. If the const variable is at the class level, mark it static const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers:

const pi = 3.14; // Unit of pressure (dynes/cm2)
const radis = 5;
const double circumference = 2 * pi * radius; // circumference

You cannot change the value of constant variable once assigned.

const array = [];
array = [5, 2, 1]; // error, const can't be reassigned a value

This was the battle of const and finals, will be present again with more interesting battles, till then stay safe and happy coding!

--

--

No responses yet