본문 바로가기
Dart (Flutter)

[Flutter] Life Cycle

by llHoYall 2022. 3. 23.

The only stateful widget has a 10-step life cycle.

The stateless widget doesn't have a life cycle because it can't be updated once it is created.

1. createState()

A class to inherit StatefulWidget class should call createState() function.

This function returns State class that contains other life cycle functions.

In other words, you can understand it as a function creating a state of a widget.

2. mounted -> true

mounted property is changed to true right after createState() is invoked.

3. initState()

initState() function is invoked once at the initializing the widget.

4. didChangeDependencies()

didChangeDependencies() function is invoked right after initState() is invoked.

5. build()

build() function renders widgets to screen.

6. didUpdateWidget()

didUpdateWidget() function is invoked when it needs to update widget after parent widget or data is changed.

7. setState()

setState() function notifies data is changed.

This function is needed to apply changed data to UI.

8. deactivate()

deactivate() function is invoked when the State instance is removed from the Flutter tree.

Corresponding memory isn't removed until dispose() function is invoked.

Therefore, the State instance can be reused before dispose() function is invoked.

9. dispose()

dispose() function is invoked to remove permanently.

10. mounted -> false

mounted property is changed to false after disappearing State instance.

Conclusion

The life cycle is important when you create an application.

Please understand it clearly.

댓글