Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's with the final/const craze in Flutter?

Tags:

flutter

dart

Java have final as well as Dart, but as far as I have seen, most Java people avoid using it all the time, since it can make your code less readable. For example, final is used all the time in class constants such as public static final int, but most people avoid using it in a method variable, since it's just seen as "excessive code correctness" by many developers, adding to boilerplate code.

C++ also has const and it can get crazy with it:

char ** const * const x // declare x as const pointer to const pointer to pointer to char

Now I am starting to learn Flutter and I am seeing final and const all over the place. Are those really necessary, like when they say:

Fields in a Widget subclass are always marked "final".

Or can they be treated as "excess of code correctness" and be removed?

Sorry if maybe my question is too stupid, I am really new to Dart and Flutter and I don't know all the side effects/benefits of using final and const, to justify the additional attention of when to remember to use them in my code.

like image 811
Michel Feinstein Avatar asked Nov 12 '18 22:11

Michel Feinstein


People also ask

What is const and final in Flutter?

The only difference between final and const is that the const makes the variable constant from compile-time only. Using const on an object, makes the object's entire deep state strictly fixed at compile-time and that the object with this state will be considered frozen and completely immutable.

Why do we use VAR final const?

The final keyword in Dart is used to create constants or objects that are immutable in nature. The only difference between the final and const keyword is that final is a runtime-constant, which in turn means that its value can be assigned at runtime instead of the compile-time that we had for the const keyword.

Why is the const important in Flutter?

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.

Why final is used in Flutter?

A variable with the final keyword will be initialized at runtime and can only be assigned for a single time. In a class and function, you can define a final variable. For Flutter specific, when the state is updated, everything in the build method will be initialized again.


2 Answers

const means that the value of the variable is known at compile time and it is going to be constant for the whole duration of the application.

Since the value is known at compile time, you can make the necessary optimisations.

final means that the value will be constant or immutable from the moment it is set. But it is set at runtime. So you don't know it at compile time and you can't optimise it.

If you don't use final you lose the immutability feature to what you should adhere in Flutter. You should always create a widget, not modify it. And the way to enforce that is to make all its fields final.

like image 53
chemamolins Avatar answered Oct 15 '22 01:10

chemamolins


All these finals are not here just for fun. Flutter revolves around immutability. final is a neat way to enforce that immutability, ensuring you are correctly following the different design patterns.

They are definitely not "excess of correctness" no. They exists to assure a maintainable app. 2 characters is absolutely worth the effort

like image 35
Rémi Rousselet Avatar answered Oct 15 '22 01:10

Rémi Rousselet