flutter 生命周期
Published:
·
LastMod: July 09, 2023
·
383 words
flutter 生命周期 🔗
createState(): When the Framework is instructed to build a StatefulWidget, it immediately callscreateState()mountedis true: WhencreateStatecreates your state class, abuildContextis assigned to that state.buildContextis, overly simplified, the place in the widget tree in which this widget is placed. Here’s a longer explanation. All widgets have abool this.mountedproperty. It is turned true when thebuildContextis assigned. It is an error to callsetStatewhen a widget is unmounted.initState(): This is the first method called when the widget is created (after the class constructor, of course.)initStateis called once and only once. It must callsuper.initState().didChangeDependencies(): This method is called immediately afterinitStateon the first time the widget is built.build(): This method is called often. It is required, and it must return a Widget.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 sameruntimeType, 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 ininitState.setState(): This method is called often from the framework itself and from the developer. Its used to notify the framework that data has changeddeactivate(): 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.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.mountedis false: The state object can never remount, and error will be thrown ifsetStateis called.
reference 🔗
https://stackoverflow.com/questions/41479255/life-cycle-in-flutter