Frank's Book
  • Introduction
  • Privacy Policy
  • Git
    • [Git]Fully delete a git repository created with init
  • iOS
    • iOS Book
      • [iOS] Swift Coding Style
      • [iOS] UnitTest With XCode
      • [iOS] Build a Universal Framework for iOS using Swift
      • [iOS] Memory Management
      • [iOS] GCD And Operation Queue
      • [iOS] MVVM
      • [iOS] CoreData
      • [iOS] KVO & KVC
      • [iOS] Asynchronous Operation
      • [iOS] URLSession
      • [iOS] NSTimer & CADisplayLink
      • [iOS] UITextField & UITestView
      • [iOS] UITableView & UICollectionView
      • [iOS] Codable
      • [iOS] Array的sort, filter, map, reduce 函式
      • [iOS] Class vs Struct
      • [iOS] Reference Type vs Value Type
      • [iOS] Object
      • [iOS] IBDesignable & IBInspectable
  • Jenkins
    • Jenkins & Docker
      • [Docker] Docker Commands
      • [Docker] 1. Setup Jenkins with Docker on Mac
      • [Docker] 2. Jenkins建立Mac子節點
      • [Docker] 3. Mac節點build Xcode project設定
      • [Jenkins] 1. Install Jenkins
      • [Jenkins] 2. Change Default User Of Jenkins
      • [Jenkins] 3. Integration with Git
      • [Jenkins] 4. Integration with XCode
      • [Jenkins] 5. Integration with Unit Test
      • [Jenkins] 6. Uninstall Jenkins
      • [Jenkins] 7. Jenkins Commands
      • [Jenkins] 8. Jenkins Plugins
      • [Jenkins] 9. Problem Solving
  • Flutter
    • Flutter Book
      • [Flutter] Update your Flutter path (Mac OS)
      • [Flutter] Release Command
      • [Flutter] Life Cycles
      • [Flutter] AppLifecycleState
      • [Flutter] Navigator Pop時回傳資料
      • [Flutter] Install appium-flutter-driver
  • Leet Code
    • LeetCode Solutions
      • [LeetCode] 1. Two Sum [Easy]
      • [LeetCode] 2. Add Two Numbers [Medium]
      • [LeetCode] 3. Longest Substring Without Repeating Characters [Medium]
      • [LeetCode] 5. Longest Palindromic Substring [Medium]
      • [LeetCode] 7. Reverse Integer [Easy] [LeetCode]
      • [LeetCode] 8. String to Integer (atoi)
      • [LeetCode] 11. Container With Most Water
      • [LeetCode] 13. Roman to Integer
      • [LeetCode]14. Longest Common Prefix
      • [LeetCode] 15. 3Sum
      • [LeetCode] 17. Letter Combinations of a Phone Number
      • [LeetCode] 19. Remove Nth Node From End of List
      • [LeetCode] 20. Valid Parentheses
      • [LeetCode] 21. Merge Two Sorted Lists
      • [LeetCode] 22. Generate Parentheses
      • [LeetCode] 26. Remove Duplicates from Sorted Array
      • [LeetCode] 28. Implement strStr()
      • [LeetCode] 33. Search in Rotated Sorted Array
      • [LeetCode] 34. Find First and Last Position of Element in Sorted Array
      • [LeetCode]36. Valid Sudoku
      • [LeetCode] 38. Count and Say
      • [LeetCode] 46. Permutations
      • [LeetCode] 48. Rotate Image
      • [LeetCode] 49. Group Anagrams
  • Git
    • Git Commands
      • [LeetCode] 50. Pow(x, n)
  • About Author
    • Frank Chen
Powered by GitBook
On this page
  1. Flutter
  2. Flutter Book

[Flutter] Life Cycles

Previous[Flutter] Release CommandNext[Flutter] AppLifecycleState

Last updated 5 years ago

1. initState()

This is the first method called when the widget is created (after the class constructor, of course.)

is called once and only once. It must also call super.initState().

This @override method is the best time to:

  1. Initialize data that relies on the specific BuildContext for the created instance of the widget.

  2. Initialize properties that rely on this widgets 'parent' in the tree.

  3. Subscribe to Streams, ChangeNotifiers, or any other object that could change the data on this widget.

@override
initState() {
  super.initState();
  // Add listeners to this class
  cartItemStream.listen((data) {
    _updateWidget(data);
  });
}

2. didChangeDependencies()

The method is called immediately after initState on the first time the widget is built.

It will also be called whenever an object that this widget depends on data from is called. For example, if it relies on an InheritedWidget, which updates.

build is always called after didChangeDependencies is called, so this is rarely needed. However, this method is the first change you have to call BuildContext.inheritFromWidgetOfExactType. This essentially would make this State 'listen' to changes on a Widget it's inheriting data from.

The docs also suggest that it could be useful if you need to do network calls (or any other expensive action) when an InheritedWidget updates.

3. build()

4. didUpdateWidget(Widget oldWidget)

This is because Flutter is re-using the state, which is long lived. In this case, required is to initialize some data again, as one would in initState().

If the state's build() method relies on a Stream or other object that can change, unsubscribe from the old object and re-subscribe to the new instance in didUpdateWidget().

tip: This method is basically the replacement for 'initState()' if it is expected the Widget associated with the widgets's state nrrds to to be rebuilt!

Flutter always called build() after this, so any subsequent further calls to setState is redundant.

@override
void didUpdateWidget(Widget oldWidget) {
  if (oldWidget.importantProperty != widget.importantProperty) {
    _init();
  }
}

5. setState()

It is used to notify the framework that "data has changed", and the widget at this build context should be rebuilt.

setState() takes a callback which cannot be async. It is for this reason it can be called often as required, because repainting is cheap :-)

void updateProfile(String name) {
 setState(() => this.name = name);
}

6. deactivate()

This is rarely used.

7. dispose()

資料來源:

This method is called often (think fps + render). It is a required, @override and must return a .

Remember that in Flutter all gui is a widget with a child or children, even , .

is called 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.

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

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.

is called when the State object is removed, which is permanent.

initState
didChangeDependencies
Widget
'Padding'
'Center'
didUpdateWidget()
'setState()'
'deactivate()'
'dispose()'
https://flutterbyexample.com/stateful-widget-lifecycle/