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 ๐Ÿ‘‡๐Ÿป
Letโ€™s start with Stateful widgets. Itโ€™s life-cycle has 8 stages:

1. createState()

2. initState()

3. didChangeDependencies()

4. build()

5. didUpdateWidget()

6. setState()

7. deactivate()

8. dispose()

Letโ€™s dive in ๐Ÿคฟ
1. createState

This is the first method that is called when you create a Stateful widget. It returns an instance of the State associated with it.

Written inside the class that extends StatefulWidget.
2. initState

This is the first method called after the widget is created. It is called only once during the entire lifetime. It is used to initialize the data that can change on the widget.

It is used to initialise controllers like AnimationController, or even Futures.
3. didChangeDependencies

This method is called immediately after initState and when dependency of the State object changes via InheritedWidget.
So, what is the difference between initState and didChangeDependencies ?

initState is called before the state loads any dependencies, due to which context is not available inside initState.

Also, we cannot call initState again and again to update variables.
didChangeDependencies is called just a few moments after the state loads its dependencies and context is available at this moment so here you can use context.

And also we can call this method any number of times to update/initialise our data.
4. build

This is where the actual widgets get rendered onto the screen. It returns the widget to be displayed.

This method is called often to refresh the ui to support dynamic changes due to user interactions or external data.
5. didUpdateWidget

This method is called every time there is a change in configuration for a given widget.

This could be something like a change in data passed through constructor from parent to child.

Let me explain this with an example:
Imagine you have a variable โ€˜titleโ€™ in the Stateful class and you update it in your State class.

The change can be detected using the code below:
oldWidget points to the previous version of the Stateful widget. The title variable is compared to check if it has changed recently.

This is the main purpose of using didUpdateWidget. It checks for change in dependencies, ie, the data passed through constructor from parent.
6. setState

This is a very popular method in Flutter. You use it to update the state of your widget.

Every time this is run, it re-renders the build method and the changes are reflected in the UI.

It is an error to call this method after the framework calls dispose.
7. deactivate

It is called when the object is removed from the tree, but donโ€™t dispose your widgets here.

It is called when you move your widget in the tree using GlobalKey
8. dispose

It is the opposite of initState. Its the last method called during the entire life cycle. It is used to dispose any variables like controllers to avoid memory leaks.
Well, thatโ€™s about Stateful widget. Letโ€™s come to Stateless widget.

It has only one method:

1. build()

It renders the stateless widget. Thatโ€™s all ๐Ÿ˜Ž
Well, thatโ€™s basically about the lifecycle of a widget in Flutter.

Hope you liked it. Do let me know if I made any mistakes anywhere, I will be happy to rectify that!

Donโ€™t forget to like and retweet ๐Ÿ™Œ๐Ÿป
Follow @GopinathanAswin for more threads like this.

I am not the best, I am an ordinary dev who loves to learn something new everyday and share it with the community ๐Ÿ’™

โ€ข โ€ข โ€ข

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

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
12 Dec
Future v/s Streams

A restaurant analogy thread ๐Ÿงต๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
Imagine you go to a restaurant and your date gives you two options:

1. Takeout and eat at your home

2. Find a table and eat there itself

Well these options in the Flutter world means:

1. Future

2. Stream
Okay! Lets see the first option: Takeout

You ordered:

1. Burgers ๐Ÿ”
2. Fries ๐ŸŸ
3. Pizza ๐Ÿ•

The waiter gave you a token and asked you to wait.

<Few moments later>

Your takeaway is ready and you both leave the place.
Read 8 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!

:(