Widget Life Cycle In Flutter
Flutter has two types of widgets Stateless Widget, and Stateful Widget.
Stateless Widget:
Stateless Widgets are rendered only once when the widget is loaded. We can't rebuild a Stateless widget based on any user events or changes.
Stateful Widgets:
Stateful Widgets can be rebuilt and have their own Widget Lifecycle to create, update and destroy the widgets.
In Flutter, widgets have several lifecycle methods that the framework calls during different stages of their lifecycle. Here are the most important ones:
createState(): This method is called during the construction stage of a stateful widget and creates a corresponding state object.
initState(): This method is called during the initialization stage of stateful widget and is used to initialize any resources the widget needs.
didChangeDependencies(): This method is called during the dependencies resolution stage of a widget, and is used to handle any changes to the widget's dependencies.
build(): This method is called during the build stage of a widget and is used to build and render the widget.
setState(): This method is called when the state of a Stateful widget changes, and triggers a rebuild of the widget.
deactivate(): This method is called when a widget is removed from the widget tree and is used to release any resources the widget no longer needs.
dispose(): This method is called when a widget is permanently removed from the widget tree and is used to release any resources the widget is holding.
These methods can be overridden in custom widget classes to provide custom behavior during different stages of the widget lifecycle.
For example, you can use the
initState()
method to initialize controllers or listeners, thebuild()
method to construct and render the widget's UI, and thesetState()
method to update the state of a Stateful widget, prompting a rebuild of the widget tree.
Happy Coding!! 🙂
Subscribe to my newsletter
Read articles from Parth Sheth directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Parth Sheth
Parth Sheth
"🚀 Fluttering through the digital cosmos, I am Parth Sheth, a passionate architect of interactive marvels in the realm of Flutter development. With a fervent love for coding and an insatiable curiosity for the ever-evolving tech landscape, I embark on a perpetual quest to redefine the boundaries of innovation. From sleek UI designs to seamless user experiences, I orchestrate symphonies of code that resonate with elegance and functionality. 💻✨ My journey in Flutter is more than just a profession; it's a thrilling odyssey where creativity meets craftsmanship. Armed with a keen eye for detail and an arsenal of cutting-edge tools, I navigate the complexities of app development with finesse, crafting solutions that not only meet but exceed expectations. 🎨🔧 But beyond the lines of code lies a deeper commitment—to inspire, empower, and elevate. Through my words, my work, and my unwavering dedication to excellence, I strive to ignite the spark of imagination in others, fostering a community where innovation knows no bounds. 🌟💡 So come, fellow adventurers, and join me on this exhilarating expedition into the unknown. Together, let's sculpt the future, one pixel at a time, and unleash the full potential of Flutter's boundless possibilities. Connect with me, and let's embark on this extraordinary voyage—together, we shall chart new horizons and redefine what's possible in the digital landscape. 🌍🔍"