MistyD
MistyD

Reputation: 17223

Why are certain variables marked as final in flutter custom classes?

I noticed in some of the examples online that classes that extend StatefulWidget have instance variables marked with final. Why is that? I understand what the final keyword does. I do not understand why it is being declared with each instance variable that extends the widget class.

Upvotes: 34

Views: 59721

Answers (5)

Snow Albert
Snow Albert

Reputation: 577

Why do we use final and what benefits can we get from it.

The core concept of Flutter's three-tree model (widget, element, render) is to avoid complicated computer restart during frame updating. So element and render tree will avoid rebuild, and widget tree as config will frequently be rebuild (widget object will frequently be create).

Therefore, the question is just like creating some object frequently, what kind of design concept are suitable for it. That's an immutable object. Think of the String class of java. The benefit is thread safe (unsafe is to share and mutable variable), no invalid state, more readable and maintainable code.

And if you have some variable state, then you should write that in your state object. The state is saved by an element object. So the element and render object is the minmum create times, they use state object to corresponding change. The widget is often created, but it is simple and immutable every time. Just like general, make command "turn right, turn left", they never say "turn ri, left".

Finally, you can read this Why are flutter widgets immutable?

Upvotes: 0

rmtmckenzie
rmtmckenzie

Reputation: 40433

Because StatefulWidget inherits Widget, which is marked as @immutable, any subclass of StatefulWidget must also be immutable (i.e. all fields final).

If you make a StatefulWidget subclass with non-final fields, it will result in this Dart analysis warning:

info: This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final). (must_be_immutable at [...] lib....dart:23)

And an explanation of how to use StatefulWidget from the StatefulWidget documentation:

StatefulWidget instances themselves are immutable and store their mutable state either in separate State objects that are created by the createState method, or in objects to which that State subscribes, for example Stream or ChangeNotifier objects, to which references are stored in final fields on the StatefulWidget itself.

Upvotes: 42

Jose Lara
Jose Lara

Reputation: 37

from what I understand from watching the boring show developed by the flutter team. Final and Const are helpful because it helps flutter know that these values wont be updated and therefore the algorithm can figure out what to update more efficiently. If i'm wrong please someone correct me

Upvotes: 0

ttfreeman
ttfreeman

Reputation: 5533

"final" is very similar to "const" in application but different for compile-time reasons: See this link or below for further explanation:

"final" means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables.

"const" has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.

Upvotes: 12

Rémi Rousselet
Rémi Rousselet

Reputation: 277077

There's no finite answer to this question. This is more of a preference.

Fact is that if a variable can be declared as final, then why not declare is as so ? final keyword doesn't hurt your code at all and can potentially help catch bugs.

In fact you can even enable a custom linter rule called prefer_final_locals which will make compilation fails if you have a non-final variable that is never reassigned.

This allows a double health check : immutable variables won't be able to change. But at the same time, if you forgot to mutate a non-final variable, the compiler will also warn you.

Upvotes: 17

Related Questions