Stateful & Stateless: A Doubt Clearing Session
As a flutter developer, you have at least once come across a question of how would you draw a line between Stateless and Stateful, either this question would have been thrown on you or it has hit your mind hard by self-realization. However, it does not matter from where you get stuck here but the main point is….. Do you have any answer to it ?
Of course in search of this mysterious question, you would find that self-proclaimed complete definition on google which is…
If a widget can change — when a user interacts with it, for example — it’s stateful. A stateless widget never changes. Icon , IconButton , and Text are examples of stateless widgets. … A widget’s state is stored in a State object, separating the widget’s state from its appearance.
But if you have ever been a part of a discussion or have appeared for an interview (like me) then you must be aware that the conversation over stateless and stateful does not end here.
whaaatttt… ???? yes, the conversation does not end here.
After giving this definition a question arrives if the stateless widget has a stateful child what it would be called? Does it become a stateful widget or how it manages itself?
So….In the layout, if we choose there can be different widgets like a container which we will select as a stateless widget and it’s child also a container but we will take as a stateful widget and when you would rebuild then the state will only affect stateful container so it does not matter if the parent widget is stateful or not if the child is a stateful widget.
Actually you can say
The only thing which makes them different is the ability to reload at the run time
But the conversation does not end here
so let’s take another definition to conquer the dilemma
The important thing to note here is at the core both Stateless and Stateful widgets behave the same. They rebuild every frame, the difference is the StatefulWidget has a State object which stores state data across frames and restores it.
If you are in doubt, then always remember this rule: If a widget changes (the user interacts with it, for example) it’s stateful. However, if a child is reacting to change, the containing parent can still be a Stateless widget if the parent doesn’t react to change.
That effort was from my side but a description is not fully completed until we don’t know the view of others so let’s have some thoughts :
In this video, a simple definition of stateful and stateless is given
Stateless is Data less and Stateful is Data full
In this video, things have described in some interesting way so first, you should know what is the state
State The state is the information that can read synchronously when the widget is built and might change during the lifetime of the widget and that state changing requirement defines if the widget is stateful or stateless.
Incidentally, the difference between the stateful and stateless widget is very limited, the stateless widget can only once on to the screen while a stateful widget can be drawn multiple times.
Which effectively means that the build function of the stateless widget can be called only once when the class is instantiated or an object is created while in stateful widget build function can be called multiple time during run time and it also states that the content in stateless is immutable while in stateful it is mutable.
The build(…) function of the StateLessWidget is called only ONCE. To redraw the StatelessWidget, we need to create a new instance of the Widget.
After reading that much of content now must b able to create your own analogy which might help you to create your own way of explaining things completely with ease.
Introduction to widgets
Flutter widgets are built using a modern framework that takes inspiration from React. The central idea is that you…flutter.dev
Summary
So this was the discussion that could help you to find your at the stage where at least you can have an explanation for the question which is generally asked during the conversation or that is asked to check one’s root knowledge.
There might be some aspects which I forgot to explain but as soon as I get to know I would try my fullest to update and as you know that this conversation began from widgets and it should go further to with different topics of flutter or as a series and in the next topic we would come with some new fundamental things.
Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.💙
If this article has helped you a bit and found interesting please clap!👏
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.