Final vs Const

A ๐Ÿงตon the battle of Immutability โš”๏ธ

#flutter #flutterdev
Immutability means you cannot change the data stored in a variable during the entire life time of your app.

But why do we use immutable variables ?
Imagine you are working on an exam portal for your school. You are given the task of building an on-screen scientific calculator.

The most common symbol you use in maths is ฯ€ (Pi) which is equal to 3.1415.

This remains the same even after 100 years right ?
What if your friend working on some other feature of the portal accidentally invokes and assigns a new value to this variable, say 10.10

Entire school is gonna fail in maths !
So, we need to make sure no one can change the value stored in the variable, here, PI

So for this very reason we use the concept of immutable variables.

In Dart, it is carried out using the โ€˜finalโ€™ and โ€˜constโ€™ keyword.
Letโ€™s see an example :
Both the variables are immutable and cannot be reassigned later.

But, there are some subtle differences between them.

๐Ÿ‘‡๐Ÿป
1. Final variables can be initialised after declaration, whereas const variables have to be initialised during declaration.

Lets see an example.

The last line PI = 3.14; will return an error, because const variables have to be initiated during declaration.
There is also a change in data type when we initialise final variables later.

When value is assigned during initialisation, the variable will have the data type of the assigned value.

But when the variable is assigned later, the datatype of the variable will be โ€˜dynamicโ€™
So, in the above code:

variable1 will be of type String, and variable2 will be of type dynamic.
2. Memory allocation

Final variables are assigned memory only when they are invoked in the program.

Const variables are assigned memory during compile-time itself.
Which means, no matter how many final variables you declare, memory will be allocated only when they are used in the program during run-time.

Whereas, const variables will consume memory even if they are never used in the program.
3. When it comes to classes, Instance variables can be final but it cannot be const (directly).

Letโ€™s see an example :
The line with the const variable will return an error : โ€œOnly static fields can be declared as const.โ€

Well, that means you just have to add a static before the const keyword:
Now, lets see something interesting.

Check out the code below, and think which line will give an error ๐Ÿค”
To be honest, these statements will execute without any errors !!

Yes, I know.

They are immutable variables but how can they be altered ??

Its simple, when it comes to lists we cannot assign a new list to an immutable type, but we can update the contents in the list !!

๐Ÿ˜Ž
Next, in the code below final statement will execute properly, but the const statement here will return an error.

This is because, you cannot assign a value to a const variable that is computed during runtime.

Any value you store should be a constant value like 1, 3.14, etc.
Well, thatโ€™s all about Final and Const in Dart.

Did I miss anything ? If so please do comment below. It will be a great help for everyone.

If you liked this ๐Ÿงต, donโ€™t forget to like and retweet so that it can reach more wonderful developers like you ๐Ÿ’™
Follow @GopinathanAswin for threads like this.

I believe in learning something new everyday, but what I love the most is sharing it with the community that gave me everything ๐Ÿ’™

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ

Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @GopinathanAswin

19 Dec
A ๐Ÿงต on my favourite 20 CLI commands for Dart and Flutter Developers!

#flutter #flutterdev
1. Create a Dart application Image
2. Run a Dart file Image
Read 23 tweets
17 Dec
Everything you need to know about Widget Lifecycle in Flutter

A thread ๐Ÿงต

#flutter #flutterdev
There are two types of Widgets in Flutter:

1. Stateless Widget : They do not change their state throughout the lifetime of your application. They are like a constant widget.

2. Stateful Widget: They are dynamic, they change their state based on the External Interactions.
A side note: Stateless widgets can change their state if some state management tools are used like Provider, Bloc, etc

But thatโ€™s out of the scope of this thread.

Letโ€™s continue ๐Ÿ‘‡๐Ÿป
Read 19 tweets
15 Dec
Mockito + Unit Testing in Flutter 101

This thread talks about Verify and Stubbing using Mockito๐Ÿงต

#flutter #flutterdev
If you are pretty new to Unit Testing, I suggest you read through my previous thread on Unit Testing:

So, what is Mockito and why do we need to use them while testing?

๐Ÿ‘‡๐Ÿป
Read 18 tweets
14 Dec
Unit Testing in Flutter 101

A mega thread ๐Ÿงต

#flutter #flutterdev
Unit Testing is the most important testing method, that is usually performed by the developer.

Unit Testing tests each individual modules (method, function or a class) for its proper functioning.
This indirectly means you have to maintain loose coupling in your application.

That is, each function or a method should be able to perform only one functionality.

This way it will be easier to test and even debug you codebase.
Read 19 tweets
13 Dec
In my latest tweet, I talked about how you can prevent multiple calls to your future methods using a variable approach.

What if I say, there is one more efficient approach to it!

Thanks to @DmytroGladkyi for telling me about it

Read on to know about it ๐Ÿ‘‡๐Ÿป

#flutterdev #flutter
The approach is using the AsyncMemoizer class.

To use the class, import the following package:
Now, letโ€™s create an instance of the AsyncMemoizer class, and initialise it inside the initState method (or you can initialise during declaration).
Read 7 tweets
13 Dec
When working with Futures and FutureBuilders, it is recommended not to execute the Future methods inside the FutureBuilder.

I learned it the hard way!

Read on to know why ๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
Every time your UI refreshes (eg: using a setState), the future method is run again, which can turn out to be very expensive!

Sometimes this could be helpful, but most of the times itโ€™s a curse for the application.

So how can you fix it ?

๐Ÿ‘‡๐Ÿป
We need to set the future in such a way that its run only once, and will never run again until you re-open the screen or you explicitly define a Swipe-down-to-refresh feature.

So, what is that one thing in Flutter, which will run only once in the entire life-cycle of your screen
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(