Why are certain variables marked as final in flutter custom classes

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

When it comes to a flutter everything you see on a screen is a widget. So in this article, we will go through why are certain variables marked as final in flutter custom classes.

Are you ready to fly with us?

What are Widgets in Flutter?

Everything in Flutter consists of widgets including but not limited to, visible Screens, Text, Buttons, Material Designs, Application Bar as well as invisible Containers and Layouts.

A flutter application is just a combination of widgets.

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

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

If you make a Stateful widget 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 Stateful Widget 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.

final” is very similar to “const” in application but different for compile-time reasons: See this link or below for a 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.

With your JavaScript/TypeScript background you were already familiar with the const keyword. In Java, you don’t have const, but the equal is final. When I started working with Dart (for Flutter) I was surprised by both of these two keywords available.

const String personConst = 'Jeroen';
final String personFinal = 'Jeroen';

personConst = 'Bob'; // Not allowed
personFinal = 'Bob'; // Not allowed

In the code above we create a const and a final variable and assign my name to both. You can’t re-assign both of them. But what is the difference?

final
  • 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. This includes all the variables with the final.
    @override
    Widget build(BuildContext context) {
      // your code
    }
    const
  • A variable with the const keyword is initialized at compile-time and is already assigned when at runtime.
  • You can’t define const inside a class. But you can in a function.
  • For Flutter specific, everything in the build method won’t be initialized again when the state is updated.
  • At runtime, Const cannot be changed.
When to use which keyword?

A simple example for both:

Use final: If you don’t know what its value will be at compile-time. For example, when you can need to get data from an API, this happens when running your code.

Use const: If you are sure that a value isn’t going to be changed when running your code. For example, when you declare a sentence that always remains the same.

final vs. var

final vs. var: When declaring variables, use final instead of var everywhere you can. This tells Dart that the variable should not be reassigned after initialization.

The code analyzer will alert you if any code attempts to set the value again.

If a variable’s value should not change after it’s initialized, Dart’s final keyword can help you avoid bugs related to unexpected mutation.

From: https://itnext.io/difference-between-const-and-final-in-dart-78c129d0c573

Conclusion:

So in this article, we took a dive into learning how certain variables marked as final in flutter custom classes.

Keep Learning !!! Keep Fluttering !!!

Don’t forget to give your feedback in the comments right below!!

Also, If you still have doubts regarding this topic. Do tell us. We would love to help 🙂

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Nirali Patel

Written by Nirali Patel

Nirali Patel is a dedicated Flutter developer with over two years of experience, specializing in creating seamless mobile applications using Dart. With a passion for crafting user-centric solutions, Nirali combines technical proficiency with innovative thinking to push the boundaries of mobile app development.

Leave a comment

Your email address will not be published. Required fields are marked *


Discuss Your Project

Connect with Flutter Agency's proficient skilled team for your app development projects across different technologies. We'd love to hear from you! Fill out the form below to discuss your project.

Have Project For Us

Get in Touch

"*" indicates required fields

ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields