ePrivacy and GPDR Cookie Consent by CookieConsent.com

Final vs Const keywords in dart

There are two confusing keywords in Dart. The first one is Final and the second is Const. Let's discuss it what are the major differences and how to use final and const keywords in the dart programming language.

In the previous article, you learned how to create a variable in the Dart. Let's learn the final and const keyword. I guarantee you that after reading these concepts will be crystal clear to you.

Compile-time vs Run-time

If you know the differences between compile-time and run-time then final and const keywords will easy for you. But if you do not know then bear with me


  • Compile-time means the time when a developer writes code and compile the code. 
  • You get programming errors when you compile the code.
    1. Syntax Errors
    2. Type checking Errors
  • Suppose you are developing an app and created an integer type variable named "age" but you assigned any string to it. At the time of compiling it will throw an error. The error is called a compile-time error.


  • Run-time means the time when the user uses the app. 
  • Run time errors are happen when the user runs the app. i.e.
    1. Division by zero
    2. Running out of memory
    3. file not found on the specified path.           
  • Suppose are asking the user to enter his age and made a variable named age which takes only integer type value. You compile the code and your code successfully compile but when the user runs the app and enters his age a non-integer value i.e. he has to enter his age in numbers but he enters any number with floating-point or enters any string. Then He will get an error and that error is called runtime-error
Now I hope runtime and compile-time topics are clear to you. 

Const keyword in dart

  • The const keyword is used for a variable whose value is declared on compile time. 
  • If the value is known at compile-time( The time of writing code ) then the const keyword will be used. i.e. ( const a  =  1; )
  • Once initialized the value of const can not change.  const a = 1;    a = 43;  This will throw an error.

Final keyword in dart

  • The Final keyword is used for a variable whose value is decided on the run-time
  • The value is not known at the compile time it is known at the run-time. For example, a variable that contains the current time is declared at the runtime ( When the user uses the app ). i.e.  final currentTime = DateTime.now();   Here the value of the variable currentTime is declared at the run time.
  • Once initialized the value of the final can not change.

Hope the concepts of final and const are clear to you.

*  *  *


  1. Thank you so much
    Finally these concepts are clear to me


Post a Comment