CONST and FINAL Variables in Dart

Kishansinh Parmar
FlutterFlakes
3 min readApr 13, 2024

--

Ok, let’s start with some baby steps first… I can create variables that can be accessible for entire the application.

var defaultPaddingVal = 16

Easy correct? Now assume you have other variables as well which help the same for applying colors, text styles, themes data and many more…

Color myPrimaryAppColor = Color(0xff006782);

TextStyle myTextStyle = TextStyle(
fontFamily: 'Roboto',
fontSize: 64,
height: 1.19,
fontWeight: FontWeight.w400,
letterSpacing: -0.5,
);

String myImagePath = 'assets/images/my_image.png';

// 100 Many mores...

Now If you run the app and use these variables it takes space in memory and is still alive unless you change the screen or remove it from the user UI stack(Talking about UI’s pages/widgets)

Dart provides two keywords, const and final, to declare variables that cannot be reassigned after initialization. Both promote immutability and improve code readability.

but there are key differences:

CONST Variables:

  • Compile-time constants: The value must be known before the app runs (e.g., mathematical constants).
  • Optimized memory usage: The compiler might inline the value, potentially reducing memory overhead at runtime (not guaranteed).
  • Initialization: Must be assigned a value at declaration.
const double pi = 3.14159;

FINAL Variables:

  • Runtime constants: The value can be assigned at runtime (e.g., user input).
  • Memory allocation: Occupy memory space during runtime after being assigned a value.
  • Initialization: This can be assigned a value later within the constructor or the scope they are declared in.
final name = getUserInput();

Remember: If you declare multiple constants with the same value, Dart will only create a single instance of that value in memory.

Benefits of const

  1. Performance: const variables are evaluated at compile-time. This means that their memory is allocated once and then reused, which can lead to performance improvements.
  2. Immutability: const creates deeply immutable values. This can be useful in many scenarios, such as when you want to ensure that a value doesn't change after it's set.
  3. Semantic Meaning: Using const can convey semantic meaning. It tells other developers that this value will not change, which can make the code easier to understand.
  4. Preventing Errors: Since const values cannot be changed once they are set, using const can help prevent bugs that could be introduced by accidentally changing a value.
  5. Use in Constant Expressions: const values can be used in constant expressions, like in const list, const map, const set, or as default values in function parameters.

Places where we can use:

Global or Local Variables: You can declare a global or local variable as const. These variables cannot be changed once they are assigned.

const pi = 3.14;

In Class: You can declare const variables inside a class, but they must be static.

class MyClass {
static const myConst = 0;
}

Default Values: const can be used to set default values in methods.

void display([String name = const 'Default Name']) {
print(name);
}

In Constructors: const can be used in constructors to make the object creation always constant.

class MyClass {
final int value;
const MyClass(this.value);
}

Constant Expressions: const can be used in constant expressions like in const list, const map, const set.

const list = [1, 2, 3];
const map = {'key': 'value'};
const set = {1, 2, 3};

I’m Kishannsinh Parmar, a Lead Flutter Developer at ketto.org with over 5 years of experience building mobile apps for startups. I specialize in building features related to payments, social interaction, and on-demand taxi services.

If you're looking to craft exceptional Flutter apps, I'd love to connect! You can find me on the following platforms:

LinkedIn: https://www.linkedin.com/in/kishansinhparmar
Github: https://github.com/imkishansinh

--

--

Kishansinh Parmar
FlutterFlakes

Lead Flutter Developer @ketto.org with 5+ years crafting startup apps. Expert in payments, social, and on-demand taxis. Let's build something extraordinary!🚀✨