Last active
May 24, 2020 19:01
-
-
Save defro/ad3d820f5cc773281d1725be51bde935 to your computer and use it in GitHub Desktop.
Life cycle in Flutter (from https://stackoverflow.com/a/53379776/3177846)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class LifeCycle extends StatefulWidget { | |
// 1 - createState(): When the Framework is instructed to build a StatefulWidget, it immediately calls createState() | |
@override | |
_LifeCycleState createState() => _LifeCycleState(); | |
} | |
class _LifeCycleState extends State<LifeCycle> { | |
// 2 - mounted is true: When createState creates your state class, a buildContext is assigned to that state. | |
// BuildContext is, overly simplified, the place in the widget tree in which this widget is placed. | |
// Here's a longer explanation: | |
// All widgets have a bool this.mounted property. | |
// It is turned true when the buildContext is assigned. | |
// It is an error to call setState when a widget is unmounted. | |
@override | |
// 3 - initState(): This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must called super.initState(). | |
void initState() { | |
super.initState(); | |
} | |
@override | |
// 4 - didChangeDependencies(): This method is called immediately after initState on the first time the widget is built. | |
void didChangeDependencies() { | |
super.didChangeDependencies(); | |
} | |
@override | |
// 5 - build(): This method is called often. It is required, and it must return a Widget. | |
Widget build(BuildContext context) { | |
return Text('Life cycle'); | |
} | |
@override | |
// 6 - didUpdateWidget(Widget oldWidget): If the parent widget changes and has to rebuild this widget | |
// (because it needs to give it different data), but it's being rebuilt with the same runtimeType, | |
// then this method is called. | |
// This is because Flutter is re-using the state, which is long lived. | |
// In this case, you may want to initialize some data again, as you would in initState. | |
void didUpdateWidget(Widget oldWidget) { | |
super.didUpdateWidget(Widget oldWidget); | |
} | |
@override | |
// 7 - setState(): This method is called often from the framework itself and from the developer. | |
// Its used to notify the framework that data has changed | |
void setState() { | |
super.setState(); | |
} | |
@override | |
// 8 - deactivate(): Deactivate is called when State is removed from the tree, but it might be reinserted | |
// before the current frame change is finished. | |
// This method exists basically because State objects can be moved from one point in a tree to another. | |
void deactivate() { | |
super.deactivate(); | |
} | |
@override | |
// 9 - dispose(): Dispose is called when the State object is removed, which is permanent. | |
// This method is where you should unsubscribe and cancel all animations, streams, etc. | |
void dispose() { | |
super.dispose(); | |
} | |
// 10 - mounted is false: The state object can never remount, and an error is thrown is setState is called. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment