Lifecycle in the State of Widget
When the Framework is instructed to build a StateFulWidget
, it immediately calls createState()
.
When CreateState
creates your state class, a BuildContext
is assigned to that state.
BuildContext
is the place in the widget tree in which this widget is placed. All widgets have a Bool this.mounted
property. It is turned true when the BuildContext
is assigned. It is an error to call setSatte
when a widget is unmounted.
# | METHOD | DESCRIPTION |
---|---|---|
1 | createState() |
When the Framework is instructed to build a StatefulWidget, it immediately calls createState() |
2 | mounted is true |
When CreateState creates your state class, a BuildContext is assigned to that state. BuildContext is the place in the widget tree in which this widget is placed. All widgets have a Bool this.mounted property. It is turned true when the BuildContext is assigned. It is an error to call setSatte when a widget is unmounted. |
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 call super.initState() . |
4 | didChangeDependencies() |
This method is called immediately after initState on the first time the widget is built. |
5 | build() |
This method is called often. It is required, and it must return a Widget . |
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 . |
7 | setState() |
This method is called often from the framework itself and from the developer. It's used to notify the framework that data has changed. |
8 | deactivate() |
deactivate() is called when the State object is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists because State objects can be moved from one point in a tree to another. |
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. |
10 | mounted is false |
The state object can never remount, and an error will be thrown if setState is called. |
App Lifecycle
For LifeCycle
, you need to use WidgetsBindingObserver
. It works when the app goes on foreground and background.
App Lifecycle Callback
Note: To catch the onResume
when you move from one screen to another, you need to use activity for result logic like:
Navigator.push(context,
MaterialPage(builder: (context) => ProductDetails(pid: productList[index]["pid"],),
settings: RouteSettings(name: '/productdetail')),).then((value){
setState(() {
length=value;
});
debugPrint('CHECK BACK FROM DETAIL $length');
});
When you press back
onPressed: (){Navigator.pop(context,length);}