Difference between Stateful and Stateless Widgets in Flutter
--
Before starting to try to make applications with Flutter, it’s good to first understand the basic concepts of its components. In flutter all components are called Widgets. Widgets have two types, namely Stateful and Stateless. Then what is the difference between Stateful and Stateless Widgets in Flutter applications? here’s the explanation.
Take my Flutter Course worth $500 for FREE → https://bit.ly/31hxS4R
→ SOURCE CODE — Car Rental App : https://codingyourlife.gumroad.com/l/flutter-cental-car-app
→ SOURCE CODE - Workout Fitness App : https://codingyourlife.gumroad.com/l/flutter-aqua-workout-app
Stateless Widget
In simple terms Stateless Widgets can be interpreted as Widgets that cannot be changed or will never change.
For example, we have a widget that contains the text “Books read: 1” then from the start the text was created until the application runs, the text will always be “Books read: 1”.
A real example in the use of Stateless Widgets is usually the “About Application” page which contains information on the name or logo of the application, version and others that are static or do not need to be changed.
Stateful Widget
Stateful Widgets are dynamic or changeable widgets. In contrast to stateless, stateful widgets can update the appearance, change colors, increase the number of rows etc. So we can conclude that any widget that can change is a stateful widget.
Referring to the previous case example with the text “Books read: 1”, then in the stateful widget we can change the text as needed. Here’s an example code and display if we want to make the numbers dynamic.
We can see in the 25th line of code that the use of the setState() function will be in charge of notifying the widget that an object has changed in the State, so that it will rebuild the Widget.
Is it possible to use the setState() function in a Stateless Widget?
No, because when you call the setState() function on a stateless widget, an error message will appear in the text editor as shown below :
Conclusion
If we want to create simple and static components then Stateless Widget is the right choice. But if you need dynamic components then use stateful widgets.
Thanks for reading !
Please to clap your hand and follow me :D
RECOMMENDED VIDEOS :
Preview Car Rental App — Flutter UI by Using GetX → https://youtu.be/tQKv40tquec
Car Rental App — Flutter UI by GetX (Part 1) → https://youtu.be/xrdqsEdLqrQ
Car Rental App — Flutter UI by GetX (Part 2) → https://youtu.be/fvpmo7MhIkc
SUPPORT ME :
Download My Apps → https://codingyourlife.gumroad.com
Buy Me a Coffee → https://www.buymeacoffee.com/faisalramdan17
My Portfolio → https://codecanyon.net/user/codingyourlife/portfolio