Immutable types in Dart / Flutter

<Blake†Codez />
3 min readAug 28, 2021

Dart and Flutter have a few different immutable types, or constants, we will explore to have a better understanding of what each does and why. An immutable type is pretty much a variable or data type than cannot be mutated, or put simply, be changed. Once that value is given, it’s given for the remainder of the application.

Dart programming language

The first immutable type in Dart / Flutter we will go over is final. Final is an immutable type, or constant, that cannot change once it is assigned.

final is different however than it’s other next of kin const which we will go over next. When using final, we are telling the application that this value will be assigned after the app has compiled in runtime.

Basically, if we have some code and the we haven’t pressed that debug play arrow yet to run the app, we are going to expect the value to be determined after the code has been compile or executed. A good way to think of this is like the examples below:

Dart — Using final in a class

The User class has 2 private properties, _id and _name. Final is used here instead of const because the values will not be determined yet until someone instantiates the User class like in the bottom of the example above. A newUser is created and cannot be changed ever because the final keyword is used.

Another example of this below:

Dart — DateTime.now()

We have an immutable variable we are defining here called time. We must assign in final because the value DateTime.now() must be executed and ran first before being determined. We will not know the true value of DateTime.now() until it has been ran. So why not const?

Dart Programming — const keyword

These above variables are assigned a const value meaning they will never change throughout the app and do not have to be compiled at runtime. const assignments are good for pre-determined values that will never change and run throughout the course of the application.

We could also make our User class in the previous examples constant as well.

Dart Programming — constant class

Now by assigning a const on our User constructor, this is called a compile-time constant. Hopefully you understand what Compile time is vs Runtime.

A cool thing to note here about const and final is that Dart has type inference. As you can see I am not putting the type after the const or final keywords, like const User newUser = User(21, ‘Bob’) or final time = DateTime.now(). Dart will automatically infer the type we are creating so if you are using an IDE, we can hover over the newUser with our mouse and see the type that it is. The type will be of type User.

Conclusion

Dart has 2 immutable types const — Compile Time and final — Run Time constants. Each one has a special meaning and Dart and can be used to generate proper syntax when writing good Clean Code in Dart.

Feel free to reach out to me on the Flutter Community Slack Channel as blakecodez. I’m also doing GDG’s in Northern California, and speaking at developer events in Redding, Ca.

Bless!

--

--

<Blake†Codez />

I’m a Software Engineering student in Redding, Ca. Love all things Computer Science related, love for journalism, Jesus Christ, and team collaboration projects.