Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Life cycle in flutter

Tags:

flutter

Does flutter have a method like Activity.resume() which can tell developer the user has gone back to the activity.

When I pick the data from internet in Page-B and go back to Page-A, how can I let Page-A know that the data is prepared.

like image 934
zhang qinglian Avatar asked Jan 05 '17 07:01

zhang qinglian


People also ask

What is life cycle of Stateful widget?

The tldr version is that State objects are long lived, but StatefulWidget s (and all Widget subclasses) are thrown away and rebuilt whenever configuration changes. It's very inexpensive ie cheap for Flutter to rebuild a mutable widget.

Which method is used in a stateless widget life cycle?

The life cycle of stateless widgets is simple; there's only one stage: the build method. As soon as the widget gets built, the build method gets automatically called where you are supposed to create whatever appearance you want to add up in your application.

What is the use of didChangeDependencies in Flutter?

According to the Flutter official docs, didChangeDependencies() is called when a dependency of the State object changes or immediately after initState(). It is safe to call BuildContext.


Video Answer


2 Answers

  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, 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.

  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. Its used to notify the framework that data has changed

  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.

  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 error will be thrown if setState is called.

like image 118
Shelly Pritchard Avatar answered Oct 03 '22 19:10

Shelly Pritchard


enter image description here Constructor

This function is not part of the life cycle, because this time the State of the widget property is empty, if you want to access the widget properties in the constructor will not work. But the constructor must be to the first call.

createState

When Flutter is instructed to build a StatefulWidget, it immediately calls createState()

Init State

Called when this object is inserted into the tree.

When inserting the render tree when invoked, this function is called only once in the life cycle. Here you can do some initialization, such as initialization State variables.

setState

The setState() method is called often from the Flutter framework itself and from the developer.

didChangeDependencies

Called when a dependency of this [State] object changes.

didUpdateWidget

Called whenever the widget configuration changes.

deactivate

Called when this object is removed from the tree. Before dispose, we will call this function.

dispose

Called when this object is removed from the tree permanently.

didChangeAppLifecycleState

Called when the system puts the app in the background or returns the app to the foreground.

Here is a good detail document: https://www.bookstack.cn/read/flutterbyexample/aebe8dda4df3319f.md

    import 'package:flutter/material.dart';      class ScreenLifecyle extends StatefulWidget {     ScreenLifecyleState state;      //createState(): When the Framework is instructed to build a StatefulWidget, it immediately calls createState()     @override     State<StatefulWidget> createState() {         // TODO: implement createState         return ScreenLifecyleState();     }     }      class ScreenLifecyleState extends State<ScreenLifecyle> {     /*     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.     mounted is false: The state object can never remount, and an error is thrown is setState is called.     */      /*     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().     */     @override     void initState() {         // TODO: implement initState         super.initState();         print("initState");     }      /*     This method is called immediately after initState on the first time the widget is built.     */     @override     void didChangeDependencies() {         // TODO: implement didChangeDependencies         super.didChangeDependencies();         print("didChangeDependencies");     }      /*     build(): This method is called often. It is required, and it must return a Widget.     */     @override     Widget build(BuildContext context) {         print("build");          // TODO: implement build         return Container();     }      /*     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.     */     @override     void didUpdateWidget(ScreenLifecyle oldWidget) {         print("didUpdateWidget");          // TODO: implement didUpdateWidget         super.didUpdateWidget(oldWidget);     }      @override     void setState(fn) {         print("setState");          // TODO: implement setState         super.setState(fn);     }      /*     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.     */     @override     void deactivate() {         // TODO: implement deactivate         print("deactivate");         super.deactivate();     }      /*     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.     */     @override     void dispose() {         // TODO: implement dispose         super.dispose();      }         @override         void didChangeAppLifecycleState(AppLifecycleState state) {             super.didChangeAppLifecycleState(state);             switch (state) {             case AppLifecycleState.inactive:                 print('appLifeCycleState inactive');                 break;             case AppLifecycleState.resumed:                 print('appLifeCycleState resumed');                 break;             case AppLifecycleState.paused:                 print('appLifeCycleState paused');                 break;             case AppLifecycleState.suspending:                 print('appLifeCycleState suspending');                 break;             }         }    }   
like image 42
Amit Prajapati Avatar answered Oct 03 '22 17:10

Amit Prajapati