Read: https://medium.com/@hadiyaaamir222/lifecycle-of-a-stateful-widget-aece2d56c946 Watch: https://www.youtube.com/watch?v=_gIbneld-bw&ab_channel=FlutterCommunity
A new Flutter project.
// https://www.tcmb.gov.tr/kurlar/today.xml
This guide provides a detailed overview of the lifecycle of a StatefulWidget
in Flutter, from creation to disposal. Understanding these steps is crucial for effective Flutter development, particularly in managing resources and maintaining performance.
- 1. Widget Creation
- 2. Initialization
- 3. Call super.initState()
- 4. Perform Custom Initialization
- 5. Widget Building
- 6. Widget Updating
- 7. Widget Rebuilding
- 8. Widget Removal
- 9. Disposing
- 10. Call super.dispose()
At this initial stage, the constructor of the widget is called. This is the phase where constants or final variables are initialized.
The initState()
method kicks in here. Set up the initial state, fetch initial data, and initialize any controllers or listeners necessary for the widget.
Within initState()
, ensure to call super.initState()
to execute the parent class's initialization procedures.
Still in the initState()
, perform any operations such as data fetching or setting up event listeners.
The build()
method constructs the widget visually based on its current state. This method represents the widget in terms of simpler, often composite widgets.
The widget updates when setState()
is invoked due to events, interactions, or incoming data, causing the build()
method to rerun and reflect state changes in the UI.
The widget continues to rebuild in response to state changes until it is deemed no longer needed.
This step occurs when the widget is removed from the widget tree, such as when navigating away from a screen or it's no longer displayed by its parent.
The dispose()
method is called, marking the end of the widget's lifecycle. Here, clean up all resources like disposing controllers, detaching listeners, closing streams, and other necessary cleanup to prevent memory leaks.
Finally, within the dispose()
method, call super.dispose()
to ensure that the base class's disposal mechanisms are executed.
Understanding and properly managing these lifecycle methods ensures efficient resource management and smooth user experiences in your Flutter applications.