What is the life cycle?

The nature of the lifecycle is the callback method. The widget’s life cycle tells us what state it is in through system-wrapped callback methods that we implement to listen for widget events, initialize data, manage memory, and so on.

What are the common callback methods?

Flutter widgets are divided into statelessWidgets and StatefulWidgets. Let’s look at their callback methods respectively.

  • StatelessWidget(Widget)

    DiscoverChildPage({this.title}){print(' constructor called '); } build @override Widget build(BuildContext context) {print('build method is called '); return Container(); }Copy the code

When I execute the Flutter project on AS, I find that the Flutter method is executed twice on startup and hot restart, but only once on Hot Reload. Why is that? I searched online for a long time but could not find the answer. Finally, I executed this project on Xcode and found that it was only executed once. It seems that this is a problem in the Debug mode of AS, so you should not worry about this problem in the future.

  • StatefulWidget (widgets, State)

    1, MinePage() {print(' Construct culvert number called! '); } 2, _MinePageState() {print('State constructor! '); } 3, @override void initState() {implement initState super.initState(); Print (init for 'State! '); } override void didChangeDependencies() {// TODO: implement didChangeDependencies super.didChangeDependencies(); } 5, @override Widget build(BuildContext context) {print('State build! '); return Container(); } 6, // State is called when it is removed from the render tree, and is about to be destroyed! @override void deactivate() { // TODO: implement deactivate super.deactivate(); } 3, @override void dispose() {// TODO: implement dispose super.dispose(); Print (' the dispose of the State! '); }Copy the code

The Widget constructor is called, the State constructor is called, init is called, build is called, and dispose is called when it is destroyed. The build method is called to refresh the interface.